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

前端 未结 2 1846
盖世英雄少女心
盖世英雄少女心 2020-12-20 13:26

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 spa

相关标签:
2条回答
  • 2020-12-20 13:45

    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', '.');
    
    0 讨论(0)
  • 2020-12-20 13:51

    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
    
    0 讨论(0)
提交回复
热议问题