gpt4 book ai didi

java - 在Java中使长变量返回十进制答案

转载 作者:太空宇宙 更新时间:2023-11-04 06:57:29 26 4
gpt4 key购买 nike

如何使 Long 返回小数?

long i = 10;
long g = 1028;

long gf =(long) i/ (long)g;

System.out.println(gf)

当前答案:

0

想要的答案:

0.001 // round to 0.000

最佳答案

整数算术会截断除法的小数部分。您需要一个 double,并且需要将一个操作数转换为 double 以创建浮点结果:

double gf = (double) i / g;

JLS Section 4.2.4定义:

If at least one of the operands to a numerical operator is of type double, then the operation is carried out using 64-bit floating-point arithmetic, and the result of the numerical operator is a value of type double. If the other operand is not a double, it is first widened (§5.1.5) to type double by numeric promotion (§5.6).

关于java - 在Java中使长变量返回十进制答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22534267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com