1
String s = "128166947252913248";
DecimalFormat df = new DecimalFormat("##########################.########");
Double oldTime1 = new Double(s); 
Double oldTime2 = new Double("128166947252913249"); 
Double oldTime3 = new Double("128166947252913247");
Double newTime = new Double("116444736000000000");
System.out.println(newTime.longValue());
System.out.println(df.format(oldTime1));
System.out.println(oldTime1-newTime);
System.out.println(df.format(oldTime1-newTime));
System.out.println(df.format(oldTime3-newTime));

输出是:

116444736000000000
128166947252913248
1.1722211252913248E16
11722211252913248
11722211252913248

这是我正在处理的代码。我无法阻止双变量在最后一个变量处被截断。请帮助我在不被截断的情况下获得价值。

4

2 回答 2

4

Double对于 18 个十进制数字没有足够的精度:64 位 IEEE-754 数字最多有 17 个十进制数字。您应该使用BigDecimal无限精度。

于 2013-02-06T00:42:37.293 回答
3

尝试使用BigDecimal

String s = "128166947252913248";
DecimalFormat df = new DecimalFormat(
        "##########################.########");
BigDecimal oldTime1 = new BigDecimal(s);
BigDecimal oldTime2 = new BigDecimal("128166947252913249");
BigDecimal oldTime3 = new BigDecimal("128166947252913247");
BigDecimal newTime = new BigDecimal("116444736000000000");
System.out.println(newTime.longValue());
System.out.println(df.format(oldTime1));
System.out.println(oldTime1.subtract(newTime));
System.out.println(df.format(oldTime1.subtract(newTime)));
System.out.println(df.format(oldTime3.subtract(newTime)));
于 2013-02-06T00:43:32.387 回答