-6

How do I convert a variable of type Money to type BigDecimal in Java?

BigDecimal big = BigDecimal.valueOf(money);

Doesn't work.

user3037540
  • 125
  • 3
  • 8

1 Answers1

2

If Money type is the one as suggested by Alexis C. in a comment above, from the Javadoc:

getAmount

java.math.BigDecimal getAmount()
Get the amount of money as a BigDecimal.
Returns: the BigDecimal amount

So you should just call BigDecimal big = money.getAmount();

Otherwise if Money is a of a different type or your own custom class then to construct BigDecimal representation you need to call the appropriate BigDecimal constructor with the relevant money amount.

Simple example:

// Assume getAmount() returns a numerical primitive type i.e. int, double, long
BigDecimal big = new BigDecimal(money.getAmount());

There are a lot of different constructors so please see the BigDecimal javadoc.

kryger
  • 12,906
  • 8
  • 44
  • 65
xlm
  • 6,854
  • 14
  • 53
  • 55