我在谷歌上搜索到的所有内容都表明,以下任何一项都会将 double 舍入到小数点后两位。
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;
但这两个都不适合我。 double 的值为 3.3743984E7
,结果相同。
怎么了?
最佳答案
没有什么不对。 3.3743984E7
是科学记数法。即:
3.3743984E7 = 3.3743984 * 10^7 = 33743984
33,743,984.0 四舍五入到小数点后两位为 33,743,984.0。 如果,也许您指定了 (由于@Sam 发现无效,上述评论已被删除。)33743984.05918
,它将四舍五入为 33743984.06
,但两个输出仍会显示 3.3743984E7
。
我can verify that your rounding code works :
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);
}
}
275914.1841 became 275914.18
我相信您只需要确定您想要什么值(value),以及您想要什么值(value)。该代码为您提供了您指定的内容。
关于java - 降低 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13943298/