Oracle 中的 BINARY_DOUBLE 和 Java 中的 Double 使用 IEEE 754 标准。
但是它们的准确性有所不同。
例如值:
456.67d
甲骨文:
declare
a BINARY_DOUBLE := 456.67d;
begin
SYS.DBMS_OUTPUT.PUT_Line(TO_CHAR(a,'9.99999999999999999999EEEE'));
end;
Result: 4.56670000000000020000E+02
Java:
Double d = 456.67d;
DecimalFormat formatter = new DecimalFormat("0.000000000000000000000E000");
System.out.println(formatter.format(d));
Result: 4.566700000000000000000E002
Java 中的值不如 Oracle 中的准确。
在线转换器表示 456.67d 最准确的表示是:
4.56670000000000015916157281026E2
那么为什么 Java 中的准确性与 Oracle 中的准确性不同呢? 我怎样才能在Java中获得更准确的值?
最佳答案
使用BigDecimal:
Double d = 456.67d;
BigDecimal bd = new BigDecimal( d );
bd.setScale( 50 );
System.out.println( bd );
DecimalFormat formatter = new DecimalFormat("0.000000000000000000000E000");
System.out.println( formatter.format( bd ) );
输出:
456.67000000000001591615728102624416351318359375
4.566700000000000159162E002
关于java - Oracle 和 Java 中 double 值的区别 (IEEE 754),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43539778/