Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the integer from BigDecimal

Tags:

java

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.

like image 540
More Than Five Avatar asked Apr 29 '19 19:04

More Than Five


People also ask

How do you get the integer part in BigDecimal?

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.

How does BigDecimal multiply to int?

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.

How do I convert BigDecimal to BigInteger?

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.

How do you find the absolute value of BigDecimal?

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.


Video Answer


1 Answers

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.

like image 187
Louis Wasserman Avatar answered Sep 23 '22 14:09

Louis Wasserman