If I have a BigDecimal with value 6.54
, how do I convert that to an integer value of 654
? i.e. no decimal point.
intValue()
will just convert to 6.
intValue() is an in-built function which converts this BigDecimal to an integer value. This function discards any fractional part of this BigDecimal. If the result of the conversion is too big to be represented as an integer value, the function returns only the lower-order 32 bits.
First off, BigDecimal. multiply() returns a BigDecimal and you're trying to store that in an int . Second, it takes another BigDecimal as the argument, not an int . If you just use the BigDecimal for all variables involved in these calculations, it should work fine.
toBigInteger() converts this BigDecimal to a BigInteger. This conversion is analogous to the narrowing primitive conversion from double to long. Any fractional part of this BigDecimal will be discarded. This conversion can lose information about the precision of the BigDecimal value.
abs() is used to return a BigDecimal whose value is the absolute value of the BigDecimal and whose scale is this. scale(). Parameters: The method does not accept any parameters. Return Value: Returns a BigDecimal whose value is the absolute value of this BigDecimal scale is this.
BigDecimal.unscaledValue()
will return a BigInteger
with the value 654
. You could then call intValue()
to convert that to an int
, if you needed that specifically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With