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

これは私が取り組んでいるコードです。double 変数が最後の変数で切り捨てられるのを止めることができません。切り捨てられずに値を取得するのを手伝ってください。

4

2 に答える 2

4

Doubleには 10 進数 18 桁の精度がありません。64 ビットの IEEE-754 数値の 10 進数は最大 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 に答える