Java:以百万为单位的格式数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/529432/
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
Java: Format number in millions
提问by Outlaw Programmer
Is there a way to use DecimalFormat (or some other standard formatter) to format numbers like this:
有没有办法使用 DecimalFormat (或其他一些标准格式化程序)来格式化数字,如下所示:
1,000,000 => 1.00M
1,234,567 => 1.23M
1,234,567,890 => 1234.57M
1,000,000 => 1.00M
1,234,567 => 1.23M
1,234,567,890 => 1234.57M
Basically dividing some number by 1 million, keeping 2 decimal places, and slapping an 'M' on the end. I've thought about creating a new subclass of NumberFormat but it looks trickier than I imagined.
基本上是将某个数字除以 100 万,保留 2 位小数,并在末尾加上一个“M”。我想过创建一个新的 NumberFormat 子类,但它看起来比我想象的要棘手。
I'm writing an API that has a format method that looks like this:
我正在编写一个 API,它的格式方法如下所示:
public String format(double value, Unit unit); // Unit is an enum
Internally, I'm mapping Unit objects to NumberFormatters. The implementation is something like this:
在内部,我将 Unit 对象映射到 NumberFormatters。实现是这样的:
public String format(double value, Unit unit)
{
NumberFormatter formatter = formatters.get(unit);
return formatter.format(value);
}
Note that because of this, I can't expect the client to divide by 1 million, and I can't just use String.format() without wrapping it in a NumberFormatter.
请注意,因此,我不能期望客户端除以 100 万,而且我不能只使用 String.format() 而不将其包装在 NumberFormatter 中。
采纳答案by jjnguy
String.format("%.2fM", theNumber/ 1000000.0);
For more information see the String.format javadocs.
有关更多信息,请参阅String.format javadocs。
回答by sblundy
Take a look at ChoiseFormat.
看看ChoiseFormat。
A more simplistic way would be to use a wrapper that auto divided by 1m for you.
更简单的方法是使用自动除以 1m 的包装器。
回答by Outlaw Programmer
Here's a subclass of NumberFormat that I whipped up. It looks like it does the job but I'm not entirely sure it's the best way:
这是我创建的 NumberFormat 的子类。看起来它可以完成这项工作,但我不完全确定这是最好的方法:
private static final NumberFormat MILLIONS = new NumberFormat()
{
private NumberFormat LOCAL_REAL = new DecimalFormat("#,##0.00M");
public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos)
{
double millions = number / 1000000D;
if(millions > 0.1) LOCAL_REAL.format(millions, toAppendTo, pos);
return toAppendTo;
}
public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos)
{
return format((double) number, toAppendTo, pos);
}
public Number parse(String source, ParsePosition parsePosition)
{
throw new UnsupportedOperationException("Not implemented...");
}
};
回答by toolkit
Why not simply?
为什么不简单?
DecimalFormat df = new DecimalFormat("0.00M");
System.out.println(df.format(n / 1000000));
回答by oxbow_lakes
Note that if you have a BigDecimal
, you can use the movePointLeft
method:
请注意,如果您有BigDecimal
,则可以使用该movePointLeft
方法:
new DecimalFormat("#.00").format(value.movePointLeft(6));
回答by Heath Borders
For now, you should use ICU's CompactDecimalFormat
, which will localize the formatting result for non-english locales. Other locales might not use a "Millions" suffix.
现在,您应该使用 ICU 的CompactDecimalFormat
,它将本地化非英语语言环境的格式化结果。其他语言环境可能不使用“Millions”后缀。
This functionality will be standard Java in JDK 12with CompactNumberFormat
.
此功能将是 JDK 12 中的标准 Java,带有CompactNumberFormat
.
回答by Muhammad Bilal ahmad
For someone looking out there to convert a given digit in human readable form.
对于那些在那里寻找以人类可读形式转换给定数字的人。
public static String getHumanReadablePriceFromNumber(long number){
if(number >= 1000000000){
return String.format("%.2fB", number/ 1000000000.0);
}
if(number >= 1000000){
return String.format("%.2fM", number/ 1000000.0);
}
if(number >= 100000){
return String.format("%.2fL", number/ 100000.0);
}
if(number >=1000){
return String.format("%.2fK", number/ 1000.0);
}
return String.valueOf(number);
}