格式化双精度到小数点后6位

sya*_*qah 2 java precision number-formatting

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

格式化为6位小数后的预期值

20790123833965.960938
Run Code Online (Sandbox Code Playgroud)

我尝试过使用十进制格式

   DecimalFormat formatter = new DecimalFormat("#0.000000");
   System.out.println(formatter.format(hashValue) );
Run Code Online (Sandbox Code Playgroud)

我得到了这个

20790123833965.960000
Run Code Online (Sandbox Code Playgroud)

Ole*_*.V. 10

正如@Benoit在评论中已经说过的那样,要保持数字的完整精度,你需要BigDecimal:

BigDecimal hashValue = new BigDecimal("20790123833965.960938");
DecimalFormat formatter = new DecimalFormat("#0.000000");
System.out.println(formatter.format(hashValue));
Run Code Online (Sandbox Code Playgroud)

输出:

20790123833965.960938