1

I am using org.javamoney.moneta library to perform some currency conversion. After the conversion, I got a number with very long digits after the comma, something like 1234.0000000000000. I would like to have only two digits after the comma. Can anyone give me some idea how to archive this?

Here is my code:

    MonetaryAmount sourceAmount= Monetary.getDefaultAmountFactory().setCurrency("USD")
            .setNumber(1234.0).create();
    CurrencyConversion targetConversion = MonetaryConversions.getConversion("EUR");
    MonetaryAmount targetAmount = source.with(targetConversion);
    System.out.println(targetAmout.toString());
Bali
  • 705
  • 4
  • 13
  • 21

1 Answers1

0

In the introduction of "JavaMoney Moneta User Guide" on GitHub there is a reference to the javax.money.format package. When you check out javadox for that package you will find MonetaryAmountFormat interface listed.

This is an example for customizing a format object:

MonetaryAmountFormat f = MonetaryFormats.getInstance(loc);

f.setStyle(f.getStyle().toBuilder().setPattern("###.##;(###.##)").build());

For further information visit:

http://javadox.com/javax.money/money-api/1.0/javax/money/format/package-summary.html

http://javadox.com/javax.money/money-api/1.0/javax/money/format/MonetaryAmountFormat.html#format-javax.money.MonetaryAmount-