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

标签 java precision number-formatting

我想将 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/

相关文章:

emacs - 使用 kB,MB,GB,... 和 kiB,MiB,GiB 的 ISO 前缀格式化整数,

java - 如何在 Java 中将选定的列从数据库获取到 JTable

java - EdgeDriver (v81) 不再支持将 DriverService 作为参数传递的功能

PHP DateInterval 创建瞬间(微秒或毫秒)间隔

android - 致命异常 : java. lang.NumberFormatException

matlab - 我如何从 1.4795e+004 转到 14795.00?

java - Spring boot RequestBody 越来越空

java - 如何从日期中获取微秒?

sqlite - CoreData NSDate SQLite 精度差异

floating-point - 请解释 What Every Computer Scientist Should Know About Floating-Point Arithmetic 中的定理 4