Java FRANCE / FRENCH Locale千位分隔符看起来像空间但实际上不是 [英] Java FRANCE/FRENCH Locale thousands separator looks like space but not actually
问题描述
如果我在FRANCE / FRENCH语言环境中转换一个数字,它应该使用空格作为千位分隔符。如果我尝试用其他字符替换空格,则找不到任何空格。
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);
输出
String after conversion in locale 123 123 123
After replace space with dot 123 123 123
因此虽然分隔符看起来像空格,但它是不同的东西。什么是确切的字符?如何在input.replace()中指定该字符,以便我可以用点替换它?
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 ?
推荐答案
正如< a href =https://stackoverflow.com/users/984823/joop-eggen> Joop Eggen 他的回答是 \ u00a0
不间断的空间。
作为一种解决方法,我得到了千分隔符如下
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, '.');
按预期工作
String after conversion in locale 123 123 123
After replace space with dot 123.123.123
这篇关于Java FRANCE / FRENCH Locale千位分隔符看起来像空间但实际上不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!