Search code examples
javajava-moneyjsr354

Is there a way to construct a MonetaryAmount from a whole cents value?


Given a price-point represented as an integer of whole cents, i.e. 199 = $1.99, is there an API method for constructing a MonetaryAmount?

One simple method would be to divide the amount by 100, but wondering if there's an API method for this.

MonetaryAmount ma = Money.of(199, "NZD").divide(100);

Solution

  • The Money.ofMinor() method is exactly what you are looking for.

    Obtains an instance of Money from an amount in minor units.
    For example, ofMinor(USD, 1234, 2) creates the instance USD 12.34