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

Considering the price presented as a whole number of whole cents, i.e. 199 = $1.99

, is there an API method to build MonetaryAmount

?

One simple method is to divide the amount by 100, but I wonder if there is an API method for that.

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

      

+3


source to share


2 answers


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



Gets an instance Money

from a sum in minor units.
For example ofMinor(USD, 1234, 2)

creates an instanceUSD 12.34

+4


source


I'm not sure if this is helpful to you. It works, however.



private void convert() {
    DecimalFormat dOffset = new DecimalFormat();
    DecimalFormat dFormat = new DecimalFormat("#,##0.00");        
    dOffset.setMultiplier(100);


    String value2, value1;

    String str;
    try {
        value1 = "0";
        value2 = dFormat.format(dOffset.parse(value1));
        System.out.println(value2);

        value1 = "7";
        value2 = dFormat.format(dOffset.parse(value1));
        System.out.println(value2);

        value1 = "04";
        value2 = dFormat.format(dOffset.parse(value1));
        System.out.println(value2);

        value1 = "123";
        value2 = dFormat.format(dOffset.parse(value1));
        System.out.println(value2);

        value1 = "123456";
        value2 = dFormat.format(dOffset.parse(value1));
        System.out.println(value2);


    } catch (ParseException ex) {
        ex.printStackTrace();
    }        
}

/* Output
    0.00
    0.07
    0.04
    1.23
    1,234.56   
*/

      

-1


source







All Articles