Problems Using DecimalFormat

I'm having trouble using DecimalFormatwhen I'm going to print the coefficients after the regression.

Here is the part of the code that is having 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 overloads on how to fix the code. I want two decimal places according to my coefficients.

thanks

Lars

+3
source share
6 answers

using:

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

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

The following snippet seems to be part of your problem:

NumberFormat , , NumberFormat's factory, (). , DecimalFormat , NumberFormat factory DecimalFormat. , - :

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

applyPattern:

applyPattern

public void applyPattern (String ) Format. - . . ; setMaximumInteger . , ,

"#, # 00.0 #" → 1,234.56

, 2 , 1 .

: "#, # 00.0 #; (#, # 00.0 #)" .

; , .

Throws: NullPointerException - IllegalArgumentException - .

+2

i18n. DecimalFormat , ,. Double.valueOf . , ..

, DecimalFormat, DecimalFormat.parse

+2

, :

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

? ? , "s" ( IMO, btw, , ) java.lang.String double?

0

.

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
source

All Articles