Recommended way to format numbers in a locale aware way?

后端 未结 3 1847
陌清茗
陌清茗 2020-12-01 15:51

Lets assume we have one million.

In English it should be formatted as 1,000,000 in German it should be 1.000.000.

相关标签:
3条回答
  • 2020-12-01 16:14

    You can achieve this with using the NumberFormat class, this also allows you to parse Strings into a local aware number.

    NumberFormat formatter = NumberFormat.getInstance(Locale.GERMAN);
    String localeFormattedNumber = formatter.format(1000000);
    
    0 讨论(0)
  • 2020-12-01 16:18

    Using NumberFormat class:

    For English:

    NumberFormat nf_us = NumberFormat.getInstance(Locale.US);
    String number_us = nf_us.format(1000000);
    

    For German:

    NumberFormat nf_ge = NumberFormat.getInstance(Locale.GERMAN);
    String number_ge = nf_ge.format(1000000);
    
    0 讨论(0)
  • 2020-12-01 16:34

    You can use NumberFormat.

    Android documentation is quite clear on it.

    0 讨论(0)
提交回复
热议问题