0

我知道如何使用以下方法打印 joda-money 对象MoneyFormatterBuilder

Money m = Money.of(CurrencyUnit.USD, 48209);
System.out.println(new MoneyFormatterBuilder().appendAmount(MoneyAmountStyle.ASCII_DECIMAL_POINT_GROUP3_COMMA).toFormatter().print(m));

但这会用小数打印:

48,209.00

如何打印没有小数的 joda-money 对象?如有必要,是否将其汇总?所以上面的输出将是:

48,209

并且(另一个例子)48209.69会打印48210

4

2 回答 2

0

您可以将钱四舍五入,然后从中获取字符串值并替换小数位。

@Test
void formatMoney() {
    Arrays.asList(10.44d, 10.45d, 10.46d, 10.50d, 10.55d).forEach(
            d -> {

                Money money = Money.of(CurrencyUnit.USD, d);

                LOGGER.info("Money with decimal: {}\tRounded money: {}\tFormatted string out of money: {}",
                    money,
                    money.rounded(0, RoundingMode.HALF_UP),                
                    money.rounded(0, RoundingMode.HALF_UP).toString().replace(".00","")); 
            }
    );
}
于 2018-08-05T05:58:35.830 回答
0

You can use BigMoney.

Try with this:

BigDecimal bigDecimal = new BigDecimal("48209.60");
BigMoney m = BigMoney.of(CurrencyUnit.USD, bigDecimal.setScale(0,RoundingMode.HALF_UP));
System.out.println(new MoneyFormatterBuilder().appendAmount(MoneyAmountStyle.ASCII_DECIMAL_POINT_GROUP3_COMMA).toFormatter().print(m));
于 2018-08-05T06:03:48.667 回答