简体   繁体   中英

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 ?

It is 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', '.');

As suggested by Joop Eggen in his answer It is 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM