如何打印格式化的BigDecimal值?

时间:2021-11-09 20:45:53

I have a BigDecimal field amount which represents money, and I need to print its value in the browser in a format like $123.00, $15.50, $0.33.

我有一个代表金钱的BigDecimal字段数量,我需要在浏览器中以$ 123.00,$ 15.50,$ 0.33的格式打印它的值。

How can I do that?

我怎样才能做到这一点?

(The only simple solution which I see myself is getting floatValue from BigDecimal and then using NumberFormat to make two-digit precision for the fraction part).

(我看到的唯一简单的解决方案是从BigDecimal获取floatValue,然后使用NumberFormat为小数部分制作两位数的精度)。

5 个解决方案

#1


100  

public static String currencyFormat(BigDecimal n) {
    return NumberFormat.getCurrencyInstance().format(n);
}

It will use your locale to choose your currency symbol. NumberFormat's javadoc

它将使用您的区域设置来选择您的货币符号。 NumberFormat的javadoc

#2


31  

Another way which could make sense for the given situation is

另一种对给定情况有意义的方法是

BigDecimal newBD = oldBD.setScale(2);

I just say this because in some cases when it comes to money going beyond 2 decimal places does not make sense. Taking this a step further, this could lead to

我只是这样说,因为在某些情况下,当涉及超过2位小数的钱时没有意义。更进一步,这可能会导致

String displayString = oldBD.setScale(2).toPlainString();

but I merely wanted to highlight the setScale method (which can also take a second rounding mode argument to control how that last decimal place is handled. In some situations, Java forces you to specify this rounding method).

但我只想突出显示setScale方法(它还可以采用第二个舍入模式参数来控制最后一个小数位的处理方式。在某些情况下,Java会强制您指定此舍入方法)。

#3


30  

To set thousand separator, say 123,456.78 you have to use DecimalFormat:

要设置千位分隔符,例如123,456.78,您必须使用DecimalFormat:

     DecimalFormat df = new DecimalFormat("#,###.00");
     System.out.println(df.format(new BigDecimal(123456.75)));
     System.out.println(df.format(new BigDecimal(123456.00)));
     System.out.println(df.format(new BigDecimal(123456123456.78)));

Here is the result:

结果如下:

123,456.75
123,456.00
123,456,123,456.78

Although I set #,###.00 mask, it successfully formats the longer values too. Note that the comma(,) separator in result depends on your locale. It may be just space( ) for Russian locale.

虽然我设置了#,###。00掩码,但它也成功地格式化了较长的值。请注意,结果中的逗号(,)分隔符取决于您的语言环境。对于俄语语言环境,它可能只是space()。

#4


6  

 BigDecimal pi = new BigDecimal(3.14);
 BigDecimal pi4 = new BigDecimal(12.56);

 System.out.printf("%.2f",pi);

// prints 3.14

//打印3.14

System.out.printf("%.0f",pi4);

// prints 13

//打印13

#5


1  

BigDecimal(19.0001).setScale(2, BigDecimal.RoundingMode.DOWN)

#1


100  

public static String currencyFormat(BigDecimal n) {
    return NumberFormat.getCurrencyInstance().format(n);
}

It will use your locale to choose your currency symbol. NumberFormat's javadoc

它将使用您的区域设置来选择您的货币符号。 NumberFormat的javadoc

#2


31  

Another way which could make sense for the given situation is

另一种对给定情况有意义的方法是

BigDecimal newBD = oldBD.setScale(2);

I just say this because in some cases when it comes to money going beyond 2 decimal places does not make sense. Taking this a step further, this could lead to

我只是这样说,因为在某些情况下,当涉及超过2位小数的钱时没有意义。更进一步,这可能会导致

String displayString = oldBD.setScale(2).toPlainString();

but I merely wanted to highlight the setScale method (which can also take a second rounding mode argument to control how that last decimal place is handled. In some situations, Java forces you to specify this rounding method).

但我只想突出显示setScale方法(它还可以采用第二个舍入模式参数来控制最后一个小数位的处理方式。在某些情况下,Java会强制您指定此舍入方法)。

#3


30  

To set thousand separator, say 123,456.78 you have to use DecimalFormat:

要设置千位分隔符,例如123,456.78,您必须使用DecimalFormat:

     DecimalFormat df = new DecimalFormat("#,###.00");
     System.out.println(df.format(new BigDecimal(123456.75)));
     System.out.println(df.format(new BigDecimal(123456.00)));
     System.out.println(df.format(new BigDecimal(123456123456.78)));

Here is the result:

结果如下:

123,456.75
123,456.00
123,456,123,456.78

Although I set #,###.00 mask, it successfully formats the longer values too. Note that the comma(,) separator in result depends on your locale. It may be just space( ) for Russian locale.

虽然我设置了#,###。00掩码,但它也成功地格式化了较长的值。请注意,结果中的逗号(,)分隔符取决于您的语言环境。对于俄语语言环境,它可能只是space()。

#4


6  

 BigDecimal pi = new BigDecimal(3.14);
 BigDecimal pi4 = new BigDecimal(12.56);

 System.out.printf("%.2f",pi);

// prints 3.14

//打印3.14

System.out.printf("%.0f",pi4);

// prints 13

//打印13

#5


1  

BigDecimal(19.0001).setScale(2, BigDecimal.RoundingMode.DOWN)