[英]Double.parseDouble loses a decimal place
I´ve encountered a problem that I can not solve. 我遇到了一个我无法解决的问题。 Tried various solutions described on the site with no result.
尝试在网站上描述的各种解决方案没有结果。 I´m passing a string “ total ” that translates to 30.00 but on Doubleparse it changes to
30.0
我传递的字符串“ total ”转换为30.00但在Doubleparse上它变为
30.0
I need the double to be 30.00
because it has to be added to an arraylist, and also displayed as text, as 30.00
and not as 30.0
我需要将double设置为
30.00
因为它必须添加到arraylist,并且还显示为文本,为30.00
而不是30.0
I´ve tried 我试过了
DecimalFormat df = new DecimalFormat("#.##");
Double number_total = Double.parseDouble(df.format(total));
ERROR: java.lang.IllegalArgumentException: Bad class: class java.lang.String at java.text.NumberFormat.format(NumberFormat.java:304) at java.text.DecimalFormat.format(DecimalFormat.java:702) at java.text.Format.format(Format.java:93) at com.example.jc.salesman.MainActivity.Totais(MainActivity.java:338) at com.example.jc.salesman.MainActivity$2.onClick(MainActivity.java:235)
错误:java.lang.IllegalArgumentException:错误的类:java的java.text.DecimalFormat.format(DecimalFormat.java:702)中的java.text.NumberFormat.format(NumberFormat.java:304)中的类java.lang.String。 com.example.jc.salesman.MainActivity.Totais(MainActivity.java:338)中的text.Format.format(Format.java:93)位于com.example.jc.salesman.MainActivity $ 2.onClick(MainActivity.java:235) )
String strSum = ((String.format("%.2f", Double.toString(sum))));
ERROR: Process: com.example.jc.salesman, PID: 26845 java.util.IllegalFormatConversionException: %f can't format
错误:进程:com.example.jc.salesman,PID:26845 java.util.IllegalFormatConversionException:%f无法格式化
java.lang.String arguments at java.util.Formatter.badArgumentType(Formatter.java:1489) at java.util.Formatter.transformFromFloat(Formatter.java:2038) at java.util.Formatter.transform(Formatter.java:1465) at java.util.Formatter.doFormat(Formatter.java:1081) at java.util.Formatter.format(Formatter.java:1042) at java.util.Formatter.format(Formatter.java:1011) at java.lang.String.format(String.java:1999) at java.lang.String.format(String.java:1973) atjava.util.Formatter.badArgumentType(Formatter.java:1489)中的java.lang.String参数,java.util.Formatter.transformFromFloat(Formatter.java:2038),java.util.Formatter.transform(Formatter.java:1465) )java.util.Formatter.formaFor(Formatter.java:1081)java.util.Formatter.format(Formatter.java:1042)java.util.Formatter.format(Formatter.java:1011)at java.lang .string.format(String.java:1999)在java.lang.String.format(String.java:1973)at at
com.example.jc.salesman.MainActivity.Totais(MainActivity.java:368)
com.example.jc.salesman.MainActivity.Totais(MainActivity.java:368)
I even forced Locale in main, 我甚至强迫Locale在主,
Locale.setDefault(new Locale("en", "US"));
Please HELP :) 请帮忙 :)
public void Total (String total){
Log.d("log2","total: " + total);// it prints 30.00
Double number_total = Double.parseDouble(total);
Log.d("log2","number_total: " + total);// it prints 30.0
losing a decimal place.
array_Total.add (number_total); //Arraylist<Double
sum (double) = sum + number_total;
String strSum = (Double.toString(sum));
total.setText (strSum);
}
You are using the df.format()
wrongly. 您错误地使用了
df.format()
。 From the javadoc , the .format() method of DecimalFormat takes a Number (double, long or Object that is a subclass of Number). 从javadoc开始 ,DecimalFormat的.format()方法接受一个Number(double,long或Object作为Number的子类)。
What you can do is to parse and store the double normally(30.0). 你可以做的是正常解析和存储双(30.0)。 When you do need to output the double, you can output using String formatter eg
String.format("%.2f", val)
当您确实需要输出double时,可以使用String格式化程序输出,例如
String.format("%.2f", val)
Use String.format("%.2f", doubleValue)
to set it to 2 decimal points. 使用
String.format("%.2f", doubleValue)
将其设置为2个小数点。 But it will roundoff your other decimal values to 2 decimal points(Eg: 30.2345 --> 30.23) as well so if you want to retain decimal values then do it conditionally. 但是它会将你的其他十进制值舍入到2个小数点(例如:30.2345 - > 30.23),所以如果你想保留十进制值,那就有条件地做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.