Search code examples
javanumber-formatting

Java Decimal Format parsing issue


public class NumFormatTest
{
    public static void main(String[] args) throws ParseException 
    {
        String num = "1 201";
        DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.FRANCE);
        System.out.println("Number Before parse: "+num);        
        double  dm = df.parse(num).doubleValue();
        System.out.println("Number After parse: "+dm);  
    }
}

Output:

 Number Before parse: 1 201

 Number After parse: 1.0

Expected Output:

  Number Before parse: 1 201

  Number After parse: **1201**

Can any please help me understand why parse is not able to convert a FRENCH locale formatted string (1 201) to normal double value (1201.0)?


Solution

  • There are two kinds of spaces. The "normal" space character (No. 32 - HEX 0x20) and the non-breaking space (NBSP) (No. 160 - HEX 0xA0).

    The French locale expects the whitespace character between the digits to be the non breaking space! You can help yourself with this line of code:

    String num = "1 201";
    num = num.replaceAll(" ", "\u00A0");    // '\u00A0' is the non breaking whitespace character!
    

    This way your code will work like expected. Please note that if you format a double into a String with French locale the resulting whitespace character will be the NBSP too!!!

    DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.FRENCH);
    System.out.println(df.format(1201.1));
    // This will print "1 202,1" But the space character will be '\u00A0'!