Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java FRANCE/FRENCH Locale thousands separator looks like space but not actually

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 ?

like image 379
Kaushik Lele Avatar asked Nov 03 '15 09:11

Kaushik Lele


2 Answers

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
like image 200
Kaushik Lele Avatar answered Oct 21 '22 21:10

Kaushik Lele


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', '.');
like image 22
Joop Eggen Avatar answered Oct 21 '22 21:10

Joop Eggen