Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract int part of a BigDecimal?

Tags:

java

In Java, I'm working with the BigDecimal class and part of my code requires me to extract the int part from it. BigDecimal does not appear to have any built in methods to help me get the number before the decimal point of a BigDecimal.

For example:

BigDecimal bd = new BigDecimal("23452.4523434");

I want to extract the 23452 from the number represented above. What's the best way to do it?

like image 243
Carlos Laspina Avatar asked Nov 17 '16 20:11

Carlos Laspina


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 do I get scale for BigDecimal?

If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. The value of the number represented by the BigDecimal is therefore (unscaledValue × 10-scale).


1 Answers

Depends on what you mean by "extract". What is the type of the result of the extraction? Another BigDecimal, a BigInteger, an int, a long, a String, or something else?

Here's code for them all:

BigDecimal result1 = bd.setScale(0, RoundingMode.DOWN);
BigInteger result2 = bd.toBigInteger();
int        result3 = bd.intValue(); // Overflow may occur
long       result4 = bd.longValue(); // Overflow may occur
String     result5 = bd.toBigInteger().toString();
String     result6 = bd.setScale(0, RoundingMode.DOWN).toString();

NumberFormat fmt = new DecimalFormat("0");
fmt.setRoundingMode(RoundingMode.DOWN);
String     result7 = fmt.format(bd);

Explanation of roundings:

  • RoundingMode.DOWN - Rounding mode to round towards zero. Never increments the digit prior to a discarded fraction (i.e., truncates). Note that this rounding mode never increases the magnitude of the calculated value.

  • toBigInteger() - Converts this BigDecimal to a BigInteger. This conversion is analogous to the narrowing primitive conversion from double to long as defined in section 5.1.3 of The Java™ Language Specification: any fractional part of this BigDecimal will be discarded. Note that this conversion can lose information about the precision of the BigDecimal value.

  • intValue() / longValue() - Converts this BigDecimal to an int / long. This conversion is analogous to the narrowing primitive conversion from double to int / long as defined in section 5.1.3 of The Java™ Language Specification: any fractional part of this BigDecimal will be discarded, and if the resulting "BigInteger" is too big to fit in an int, only the low-order 32 / 64 bits are returned.

As can be seen from the descriptions, all 4 discards fractional part, i.e. rounds towards zero, aka truncates the value.

like image 112
Andreas Avatar answered Sep 25 '22 20:09

Andreas