Java FRANCE/FRENCH Locale thousands separator look

2019-01-27 19:43发布

问题:

If I convert a number in FRANCE/FRENCH locale it should use space as thousands separator. If I try to replace the spaces with some other characters, it does not find any space.

String input = NumberFormat.getNumberInstance(Locale.FRANCE).format(123123123);
System.out.println("String after conversion in locale "+input);
input = input.replace(" ", ".");
System.out.println("After replace space with dot      "+input);

OUTPUT

String after conversion in locale 123 123 123
After replace space with dot      123 123 123

So though separator looks like space it is something different. What is the exact character ? How can I specify that character in input.replace() so that I can replace it with dot ?

回答1:

It is \u00a0 the non-breaking space, which makes sense for an amount. (Imagine € 40 at the end of a line, and the next starting with 000.) The same holds for some other languages.

input = input.replace('\u00a0', '.');


回答2:

As suggested by Joop Eggen in his answer It is \u00a0 the non-breaking space.

As a work around, I got the thousand separator as follows

String input = NumberFormat.getNumberInstance(Locale.FRANCE).format(123123123);
System.out.println("String after conversion in locale "+input);

DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.FRANCE);
DecimalFormatSymbols symbols = df.getDecimalFormatSymbols();
char thousandSep = symbols.getGroupingSeparator();

input = input.replace(thousandSep, '.'); 

It worked as expected

String after conversion in locale 123 123 123
After replace space with dot      123.123.123