使用 DecimalFormat 的问题

发布于 2024-09-19 20:51:08 字数 751 浏览 4 评论 0原文

当我要在回归后打印系数时,我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

捂风挽笑 2024-09-26 20:51:09

您是否正在尝试格式化该数字?还是圆的?如果你正在格式化它,你的“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 a double?

挽清梦 2024-09-26 20:51:09

检查您的区域设置。

DecimalFormat twoDForm = new DecimalFormat("0.00");   
    private double s(double d){ 

    String doubleString =  displayNumberAmount(twoDForm.format(d));
        return Double.valueOf(doubleString);  
    } 

    public static String displayNumberAmount(String amount) {

            NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.CANADA_FRENCH);

            Number number = 0;

            try {
                number = numberFormat.parse(amount);

            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return String.format(Locale.US, "%1$,.2f", number);
        }

Check your Locale.

DecimalFormat twoDForm = new DecimalFormat("0.00");   
    private double s(double d){ 

    String doubleString =  displayNumberAmount(twoDForm.format(d));
        return Double.valueOf(doubleString);  
    } 

    public static String displayNumberAmount(String amount) {

            NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.CANADA_FRENCH);

            Number number = 0;

            try {
                number = numberFormat.parse(amount);

            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return String.format(Locale.US, "%1$,.2f", number);
        }
一个人的旅程 2024-09-26 20:51:08

使用:

    DecimalFormat twoDForm = new DecimalFormat("#.##");
    DecimalFormatSymbols dfs = new DecimalFormatSymbols();
    dfs.setDecimalSeparator('.');
    twoDForm.setDecimalFormatSymbols(dfs);

use:

    DecimalFormat twoDForm = new DecimalFormat("#.##");
    DecimalFormatSymbols dfs = new DecimalFormatSymbols();
    dfs.setDecimalSeparator('.');
    twoDForm.setDecimalFormatSymbols(dfs);
月亮是我掰弯的 2024-09-26 20:51:08

http://download.oracle.com/ javase/1.4.2/docs/api/java/text/DecimalFormat.html

以下摘录似乎是您问题的一部分:

获取 NumberFormat
特定区域设置,包括默认区域设置
区域设置,调用 NumberFormat 之一
工厂方法,例如
getInstance()。一般情况下不要打电话
DecimalFormat 构造函数
直接,因为 NumberFormat
工厂方法可能返回子类
除了 DecimalFormat 之外。如果您需要
要自定义格式对象,请执行以下操作
像这样的东西:

 NumberFormat f = NumberFormat.getInstance(loc);
 if (f instanceof DecimalFormat) {
     ((DecimalFormat) f).setDecimalSeparatorAlwaysShown(true);
 }

您可能想要使用 applyPattern 方法:

应用模式

公共无效applyPattern(字符串
模式)将给定的模式应用到
这个格式对象。一个模式是一个
的简写规范
各种格式属性。这些
属性也可以改变
分别通过各种
设置器方法。没有限制
整数位由此设置
例行公事,因为这是典型的
最终用户的愿望;使用setMaximumInteger
如果你想设置一个实际值。为了
负数,使用第二个
模式,用分号分隔

示例“#,#00.0#”-> 1,234.56

这意味着至少 2 个整数
数字、1 个小数位和一个
最多 2 个小数位。

示例:“#,#00.0#;(#,#00.0#)”
括号内为负数。

在负模式中,最小值和
最大计数被忽略;这些都是
假定设置为正
模式。

抛出:NullPointerException - 如果
模式为空
IllegalArgumentException - 如果
给定的模式无效。

http://download.oracle.com/javase/1.4.2/docs/api/java/text/DecimalFormat.html

The following excerpt appears to be part of your problem:

To obtain a NumberFormat for a
specific locale, including the default
locale, call one of NumberFormat's
factory methods, such as
getInstance(). In general, do not call
the DecimalFormat constructors
directly, since the NumberFormat
factory methods may return subclasses
other than DecimalFormat. If you need
to customize the format object, do
something like this:

 NumberFormat f = NumberFormat.getInstance(loc);
 if (f instanceof DecimalFormat) {
     ((DecimalFormat) f).setDecimalSeparatorAlwaysShown(true);
 }

You may want to use the applyPattern method:

applyPattern

public void applyPattern(String
pattern) Apply the given pattern to
this Format object. A pattern is a
short-hand specification for the
various formatting properties. These
properties can also be changed
individually through the various
setter methods. There is no limit to
integer digits are set by this
routine, since that is the typical
end-user desire; use setMaximumInteger
if you want to set a real value. For
negative numbers, use a second
pattern, separated by a semicolon

Example "#,#00.0#" -> 1,234.56

This means a minimum of 2 integer
digits, 1 fraction digit, and a
maximum of 2 fraction digits.

Example: "#,#00.0#;(#,#00.0#)" for
negatives in parentheses.

In negative patterns, the minimum and
maximum counts are ignored; these are
presumed to be set in the positive
pattern.

Throws: NullPointerException - if
pattern is null
IllegalArgumentException - if the
given pattern is invalid.

£噩梦荏苒 2024-09-26 20:51:08

您遇到了 i18n 问题。 DecimalFormat 使用默认区域设置,将小数分隔符指定为 ,。但是,Double.valueOf 不使用区域设置。它始终期望小数点分隔符为 .

如果要解析使用 DecimalFormat 格式化的字符串,则需要使用 DecimalFormat.parse

You are encountering an i18n issue. DecimalFormat is using your default locale which specifies the decimal separator as ,. However, the Double.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

猥︴琐丶欲为 2024-09-26 20:51:08

我认为你打算做的是:

private static String s(double d) {
   return twoDForm.format(d);
}

I think what you intended to do is:

private static String s(double d) {
   return twoDForm.format(d);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文