使用 DecimalFormat 的问题
当我要在回归后打印系数时,我在使用 DecimalFormat 时遇到问题。
这是遇到问题的代码部分;
DecimalFormat twoDForm = new DecimalFormat("0.00");
private double s(double d){
return Double.valueOf(twoDForm.format(d));
}
这是 eclipse 中的错误消息;
Exception in thread "main" java.lang.NumberFormatException: For input string: "0,16"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.valueOf(Unknown Source)
at model.ARF2.s(ARF2.java:126)
at model.ARF2.printBestModel(ARF2.java:114)
at testing.testclass3.bestForecastingModel(testclass3.java:69)
at testing.testclass3.main(testclass3.java:36)
如果有人对如何修复代码有任何疑问,请告诉我。我想要我的系数保留两位小数。
谢谢拉尔斯
I am having problems using DecimalFormat
when I am going to print out coefficients after a regression.
Here is the part of the code that is facing problems;
DecimalFormat twoDForm = new DecimalFormat("0.00");
private double s(double d){
return Double.valueOf(twoDForm.format(d));
}
and here is the error message in eclipse;
Exception in thread "main" java.lang.NumberFormatException: For input string: "0,16"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.valueOf(Unknown Source)
at model.ARF2.s(ARF2.java:126)
at model.ARF2.printBestModel(ARF2.java:114)
at testing.testclass3.bestForecastingModel(testclass3.java:69)
at testing.testclass3.main(testclass3.java:36)
Please let me know if anyone has any surgestions on how to fix the code. I want two decimals on my coefficients.
Thank you
Lars
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您是否正在尝试格式化该数字?还是圆的?如果你正在格式化它,你的“s”方法(IMO,名字不好,顺便说一句,但它是私有的,所以这是你的电话)不应该返回一个
java.lang.String
而不是双?
Are you trying to format the number? Or round it? If you're formatting it, shouldn't your "s" method (bad name IMO, btw, but it's private, so it's your call) return a
java.lang.String
instead of adouble
?检查您的区域设置。
Check your Locale.
使用:
use:
http://download.oracle.com/ javase/1.4.2/docs/api/java/text/DecimalFormat.html
以下摘录似乎是您问题的一部分:
您可能想要使用 applyPattern 方法:
http://download.oracle.com/javase/1.4.2/docs/api/java/text/DecimalFormat.html
The following excerpt appears to be part of your problem:
You may want to use the applyPattern method:
您遇到了 i18n 问题。
DecimalFormat
使用默认区域设置,将小数分隔符指定为 ,。但是,Double.valueOf
不使用区域设置。它始终期望小数点分隔符为 .。如果要解析使用
DecimalFormat
格式化的字符串,则需要使用 DecimalFormat.parseYou are encountering an i18n issue.
DecimalFormat
is using your default locale which specifies the decimal separator as ,. However, theDouble.valueOf
does not use the locale. It always expects that the decimal separator is ..If you want to parse a string formatted with
DecimalFormat
then you need to use DecimalFormat.parse我认为你打算做的是:
I think what you intended to do is: