3

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

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
Lars
  • 31
  • 1
  • 1
  • 2
  • Is the dot correct? Shouldn't it be `DecimalFormat twoDForm = new DecimalFormat("0,00");`? I do not remember right now. – Alberto Zaccagni Sep 17 '10 at 13:21
  • what is the input into the method `s`? – dogbane Sep 17 '10 at 13:22
  • The code is all wrong, but the correct answer depends on what you want to do. Do you want to print just two decimal places, truncating the rest (effectively rounding down)? Or do you want to use a specific method for rounding that you want to use when there are more digits than you have decimal places for printing? – Jeff Sep 17 '10 at 13:33

6 Answers6

19

use:

    DecimalFormat twoDForm = new DecimalFormat("#.##");
    DecimalFormatSymbols dfs = new DecimalFormatSymbols();
    dfs.setDecimalSeparator('.');
    twoDForm.setDecimalFormatSymbols(dfs);
Miky
  • 942
  • 2
  • 14
  • 29
2

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.

Thomas Langston
  • 3,743
  • 1
  • 25
  • 41
2

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

Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71
1

I think what you intended to do is:

private static String s(double d) {
   return twoDForm.format(d);
}
nanda
  • 24,458
  • 13
  • 71
  • 90
0

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);
        }
0

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?

Jack Leow
  • 21,945
  • 4
  • 50
  • 55