打印不带小数的jodamoney

时间:2018-08-05 04:26:14

标签: java joda-money

我知道如何使用MoneyFormatterBuilder打印一个joda-money对象:

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

2 个答案:

答案 0 :(得分: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","")); 
            }
    );
}

答案 1 :(得分:0)

您可以使用BigMoney

尝试一下:

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