jim*_*mmy 36 java double rounding numberformatexception
我有一个双值= 1.068879335
我想用它只有两个十进制值,如1.07.
我试过这样的
DecimalFormat df=new DecimalFormat("0.00");
String formate = df.format(value);
double finalValue = Double.parseDouble(formate) ;
Run Code Online (Sandbox Code Playgroud)
这给了我以下例外情况
java.lang.NumberFormatException: For input string: "1,07"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
at java.lang.Double.parseDouble(Double.java:510)
Run Code Online (Sandbox Code Playgroud)
可以有人告诉我我的代码有什么问题.
最后我需要finalValue = 1.07;
biz*_*lop 79
请注意字符串中的逗号:"1,07".DecimalFormat
使用特定于语言环境的分隔符字符串,而Double.parseDouble()
不是.当您碰巧住在小数分隔符为","的国家/地区时,您无法解析您的号码.
但是,您可以使用DecimalFormat
它来解析它:
DecimalFormat df=new DecimalFormat("0.00");
String formate = df.format(value);
double finalValue = (Double)df.parse(formate) ;
Run Code Online (Sandbox Code Playgroud)
但你真的应该这样做:
double finalValue = Math.round( value * 100.0 ) / 100.0;
Run Code Online (Sandbox Code Playgroud)
注意:正如已经指出的那样,如果您不需要精确控制精度,则应该只使用浮点数.(财务计算是何时不使用它们的主要例子.)
Pet*_*rey 20
直播@ Sergey的解决方案,但整数除法.
double value = 23.8764367843;
double rounded = (double) Math.round(value * 100) / 100;
System.out.println(value +" rounded is "+ rounded);
Run Code Online (Sandbox Code Playgroud)
版画
23.8764367843 rounded is 23.88
Run Code Online (Sandbox Code Playgroud)
编辑:正如谢尔盖指出的那样,将double*int和double*double乘以double/int和double/double之间应该没有区别.我找不到结果不同的例子.但是在x86/x64和其他系统上,有一个特定的机器代码指令,用于混合的double,int值,我相信JVM使用它.
for (int j = 0; j < 11; j++) {
long start = System.nanoTime();
for (double i = 1; i < 1e6; i *= 1.0000001) {
double rounded = (double) Math.round(i * 100) / 100;
}
long time = System.nanoTime() - start;
System.out.printf("double,int operations %,d%n", time);
}
for (int j = 0; j < 11; j++) {
long start = System.nanoTime();
for (double i = 1; i < 1e6; i *= 1.0000001) {
double rounded = (double) Math.round(i * 100.0) / 100.0;
}
long time = System.nanoTime() - start;
System.out.printf("double,double operations %,d%n", time);
}
Run Code Online (Sandbox Code Playgroud)
打印
double,int operations 613,552,212
double,int operations 661,823,569
double,int operations 659,398,960
double,int operations 659,343,506
double,int operations 653,851,816
double,int operations 645,317,212
double,int operations 647,765,219
double,int operations 655,101,137
double,int operations 657,407,715
double,int operations 654,858,858
double,int operations 648,702,279
double,double operations 1,178,561,102
double,double operations 1,187,694,386
double,double operations 1,184,338,024
double,double operations 1,178,556,353
double,double operations 1,176,622,937
double,double operations 1,169,324,313
double,double operations 1,173,162,162
double,double operations 1,169,027,348
double,double operations 1,175,080,353
double,double operations 1,182,830,988
double,double operations 1,185,028,544
Run Code Online (Sandbox Code Playgroud)
问题是您使用本地化格式化程序生成特定于语言环境的小数点,在您的情况下为",".但Double.parseDouble()需要非本地化的双字面值.您可以通过使用特定于语言环境的解析方法或将格式化程序的语言环境更改为使用"."的方法来解决您的问题.作为小数点.或者甚至更好,通过使用这样的东西避免不必要的格式化:
double rounded = (double) Math.round(value * 100.0) / 100.0;
Run Code Online (Sandbox Code Playgroud)