gpt4 book ai didi

java - 格式化 double 到 6 位小数精度

转载 作者:行者123 更新时间:2023-12-01 01:25:03 24 4
gpt4 key购买 nike

我想将 double 值格式化为 6 位精度而不进行舍入。

格式化后的期望值到小数点后 6 位

20790123833965.960938

我试过使用十进制格式
   DecimalFormat formatter = new DecimalFormat("#0.000000");
System.out.println(formatter.format(hashValue) );

我得到了这个
20790123833965.960000

最佳答案

@Benoit已经在评论中说,要保持您的号码的完整精度,您需要一个 BigDecimal :

BigDecimal hashValue = new BigDecimal("20790123833965.960938");
DecimalFormat formatter = new DecimalFormat("#0.000000");
System.out.println(formatter.format(hashValue));

输出:

20790123833965.960938

关于java - 格式化 double 到 6 位小数精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53496080/

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