降低双倍的准确性

Dea*_*ely 1 java android

我发现谷歌搜索的所有内容都表明,以下任何一种方法都会将双倍减少到两位小数.

double roundToFourDecimals(double d)
{
    DecimalFormat twoDForm = new DecimalFormat("#.##");
    double myD = Double.valueOf(twoDForm.format(d));
    return myD;
}

double nextLon = (double)Math.round(bnextLon * 100.0) / 100.0;
Run Code Online (Sandbox Code Playgroud)

但这对我都不起作用.double的值是3.3743984E7,结果是相同的.怎么了?

Eri*_*ric 5

没错.3.3743984E7是科学记数法.那是:

3.3743984E7 = 3.3743984 * 10^7 = 33743984
Run Code Online (Sandbox Code Playgroud)

33,743,984.0四舍五入到小数点后两位是33,743,984.0.如果,或许你指定33743984.05918,它将四舍五入33743984.06,但两个输出仍然会说3.3743984E7.(由于@Sam发现无效,前面的评论已被删除.)

可以验证您的舍入代码是否有效:

public class Main {
    public static void main(String[] args) {
        double bnextLon = 275914.18410;
        double nextLon = (double) Math.round(bnextLon * 100.0) / 100.0;
        System.out.println(bnextLon + " became " + nextLon);
    }
}
Run Code Online (Sandbox Code Playgroud)

275914.1841 became 275914.18

我相信你只需要确定你想要的价值,以及你想要的价值.代码正在为您提供您正在指定的内容.