Java 以区域设置感知方式格式化数字的推荐方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4375410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 16:41:05  来源:igfitidea点击:

Recommended way to format numbers in a locale aware way?

javaandroid

提问by mibollma

Lets assume we have one million.

假设我们有一百万。

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

在英语中,它应该像1,000,000在德语中一样格式化1.000.000

采纳答案by Jesus Oliva

Using NumberFormatclass:

使用NumberFormat类:

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);

回答by Riduidel

You can use NumberFormat.

您可以使用NumberFormat

Android documentation is quite clear on it.

Android 文档对此非常清楚。

回答by Christopher Klewes

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

您可以使用NumberFormat类来实现这一点,这也允许您将字符串解析为本地感知数字。

NumberFormat formatter = NumberFormat.getInstance(Locale.GERMAN);
String localeFormattedNumber = formatter.format(1000000);