Java: Format number in millions

半腔热情 提交于 2019-11-27 05:55:50

问题


Is there a way to use DecimalFormat (or some other standard formatter) to format numbers like this:

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.

I'm writing an API that has a format method that looks like this:

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:

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.


回答1:


String.format("%.2fM", theNumber/ 1000000.0);

For more information see the String.format javadocs.




回答2:


Note that if you have a BigDecimal, you can use the movePointLeft method:

new DecimalFormat("#.00").format(value.movePointLeft(6));



回答3:


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:

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...");
    }
};



回答4:


Why not simply?

DecimalFormat df = new DecimalFormat("0.00M");
System.out.println(df.format(n / 1000000));



回答5:


Take a look at ChoiseFormat.

A more simplistic way would be to use a wrapper that auto divided by 1m for you.




回答6:


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.

This functionality will be standard Java in JDK 12 with CompactNumberFormat.




回答7:


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

}


来源:https://stackoverflow.com/questions/529432/java-format-number-in-millions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!