Java:以百万为单位的格式数字

发布于 2024-07-13 05:42:33 字数 704 浏览 6 评论 0原文

有没有办法使用 DecimalFormat (或其他标准格式化程序)来格式化数字,如下所示:

1,000,000 => 100万

1,234,567 => 123万

1,234,567,890 => 1234.57M

基本上将某个数字除以 100 万,保留 2 位小数,并在末尾加上“M”。 我曾考虑过创建 NumberFormat 的新子类,但它看起来比我想象的要棘手。

我正在编写一个 API,它具有如下所示的格式方法:

public String format(double value, Unit unit); // Unit is an enum

在内部,我将 Unit 对象映射到 NumberFormatters。 实现是这样的:

public String format(double value, Unit unit)
{
    NumberFormatter formatter = formatters.get(unit);
    return formatter.format(value);
}

请注意,正因为如此,我不能期望客户端除以 100 万,而且我不能只使用 String.format() 而不将其包装在 NumberFormatter 中。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

梨涡 2024-07-20 05:42:33
String.format("%.2fM", theNumber/ 1000000.0);

有关详细信息,请参阅 String.format javadocs

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

For more information see the String.format javadocs.

自由范儿 2024-07-20 05:42:33

请注意,如果您有 BigDecimal,则可以使用 movePointLeft 方法:

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

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

new DecimalFormat("#.00").format(value.movePointLeft(6));
甜妞爱困 2024-07-20 05:42:33

对于那些寻找将给定数字转换为人类可读形式的人来说。

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

}

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

}
べ繥欢鉨o。 2024-07-20 05:42:33

这是我创建的 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...");
    }
};

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...");
    }
};
后eg是否自 2024-07-20 05:42:33

为什么不简单呢?

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

Why not simply?

DecimalFormat df = new DecimalFormat("0.00M");
System.out.println(df.format(n / 1000000));
转角预定愛 2024-07-20 05:42:33

在 Kotlin 语言中,您可以创建扩展函数:

fun Long.formatToShortNumber(): String {
    return when {
        this >= 1000000000 -> String.format("%.2fB", this / 1000000000.0)
        this >= 1000000 -> String.format("%.2fM", this / 1000000.0)
        this >= 1000 -> String.format("%.2fK", this / 1000.0)
        else -> this.toString()
    }
}

In Kotlin language, you can make extention function:

fun Long.formatToShortNumber(): String {
    return when {
        this >= 1000000000 -> String.format("%.2fB", this / 1000000000.0)
        this >= 1000000 -> String.format("%.2fM", this / 1000000.0)
        this >= 1000 -> String.format("%.2fK", this / 1000.0)
        else -> this.toString()
    }
}
若相惜即相离 2024-07-20 05:42:33

查看 ChoiseFormat

更简单的方法是使用自动除以 1m 的包装器。

Take a look at ChoiseFormat.

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

为你鎻心 2024-07-20 05:42:33

目前,您应该使用 ICU 的 CompactDecimalFormat< /code>,它将本地化​​非英语语言环境的格式化结果。 其他区域设置可能不使用“Millions”后缀。

此功能将成为 JDK 12 中的标准 Java 以及 CompactNumberFormat.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文