我有一个 Double
对象,当转换为 long
值时,会失去精确值。
Double d = 1.14*100
System.out.println(d.longValue());
上述语句将打印:
113
。
我希望打印114
。我有一个 Double
对象,当转换为 long
值时,会失去精确值。
Double d = 1.14*100
System.out.println(d.longValue());
113
。
我希望打印114
。Math.round
:double d = 1.14*100;
System.out.println(Math.round(d));
如果您需要表示您期望的整数/长整数值,可以使用以下方法:
Math.round(1.14 * 100)
Long.parseLong( String.format( "%.0f",doublevalue ) ) ;
double
不是精确的。接下来,在将 double
/float
强制转换为 long
/int
时,小数部分会被舍弃(而不是四舍五入)。System.out.println(Math.round(d));
BigDecimal
。 - Pshemo