Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 663
Brett Ryan Avatar asked Mar 15 '23 04:03

Brett Ryan


1 Answers

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

like image 130
Sergey Ponomarev Avatar answered Apr 27 '23 22:04

Sergey Ponomarev