2

I try to get a Double-Value of a Number which is formatted in French style. So the Text 1 003,25 should stored as value (1003.25).

public class NumberFormatTest { 
    public static void main(String[] args) throws ParseException {
        String db = "1003.25";
        String manualFRA = "1 003,25";
        double numberDB = Double.parseDouble(db);
        String ger = NumberFormat.getInstance(Locale.GERMAN).format(numberDB);
        String fra = NumberFormat.getInstance(Locale.FRENCH).format(numberDB);
        String eng = NumberFormat.getInstance(Locale.ENGLISH).format(numberDB);

        double gerD = NumberFormat.getNumberInstance(Locale.GERMAN).parse(ger).doubleValue();
        double fraD = NumberFormat.getNumberInstance(Locale.FRENCH).parse(fra).doubleValue();
        double mfraD = NumberFormat.getNumberInstance(Locale.FRENCH).parse(manualFRA).doubleValue();
        double engD = NumberFormat.getNumberInstance(Locale.ENGLISH).parse(eng).doubleValue();

        System.out.println("From database: " + db);

        System.out.println("\tGerman: " + ger);
        System.out.println("\tFrench: " + fra);
        System.out.println("\tEnglish: " + eng);

        System.out.println("\nTo Database: ");  
        System.out.println("\tfrom German: "+ gerD);
        System.out.println("\tfrom French: "+ fraD);
        System.out.println("\tfrom manual French: "+ mfraD);
        System.out.println("\tfrom Englisch: "+ engD);
    }
}

From database: 1003.25
 German: 1.003,25
 French: 1 003,25
 English: 1,003.25

To Database: 
 from German: 1003.25
 from French: 1003.25
 from manual French: 1.0
 from Englisch: 1003.25

Does anybody has an idea, why the manual French value is 1.0 and not 1003.25? How can I solve it? In the real program the value comes as String from an TextField.

Thanks in advance for your help!

Taros
  • 70
  • 6

1 Answers1

4

The space in the French formatted text is a NO-BREAK SPACE, not a regular space.

If you change string literal to "1\u00a0003,25", it will work.

If that text comes from external source, e.g. user, then you could just replace spaces with no-break spaces, e.g.

parse(manualFRA.replace(' ', '\u00a0'))
Andreas
  • 154,647
  • 11
  • 152
  • 247