I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we’ll explore various methods to separate integer and decimal parts of floating-point types in Java, namely float and double.

2. Issues with Floating-Points Types

Let’s start by looking at a simple fraction, and a naive way of performing the separation, via casting:

double doubleNumber = 24.04;
int intPart = (int) doubleNumber;
System.out.println("Double Number: " + doubleNumber);
System.out.println("Integer Part: " + intPart);
System.out.println("Decimal Part: " + (doubleNumber - intPart));

When we try to run the code above, here’s what we get:

Double Number: 24.04
Integer Part: 24
Decimal Part: 0.03999999999999915

Contrary to our expectation, the output does not print the decimal part correctly. Hence, the Floating-point numbers aren’t suitable for calculations where roundoff errors cannot be tolerated.

3. First Approach: Splitting the String

First of all, let’s convert the decimal number to a String equivalent. Then we can split the String at the decimal point index.

Let’s understand this with an example:

double doubleNumber = 24.04;
String doubleAsString = String.valueOf(doubleNumber);
int indexOfDecimal = doubleAsString.indexOf(".");
System.out.println("Double Number: " + doubleNumber);
System.out.println("Integer Part: " + doubleAsString.substring(0, indexOfDecimal));
System.out.println("Decimal Part: " + doubleAsString.substring(indexOfDecimal));

The output of the code above is:

Double Number: 24.04
Integer Part: 24
Decimal Part: .04

The output is exactly what we expect. But, the problem here is the limitation of using a String – which means we won’t be able to now perform any other arithmetic operations it.

4. Second Approach: Using BigDecimal

The BigDecimal class in Java provides its user with complete control over rounding behavior. This class also provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion.

Let’s use BigDecimal to get the integer and decimal parts of a floating point number:

double doubleNumber = 24.04;
BigDecimal bigDecimal = new BigDecimal(String.valueOf(doubleNumber));
int intValue = bigDecimal.intValue();
System.out.println("Double Number: " + bigDecimal.toPlainString());
System.out.println("Integer Part: " + intValue);
System.out.println("Decimal Part: " + bigDecimal.subtract(
  new BigDecimal(intValue)).toPlainString());

The output will be:

Double Number: 24.04
Integer Part: 24
Decimal Part: 0.04

As we can see above, the output is as expected. We can also perform arithmetic operations with the help of methods provided in BigDecimal class.

5. Conclusion

In this article, we discussed various methods to separate integer and decimal parts of floating-point types. We also discussed the benefits of using BigDecimal for floating point calculations.

Also, our detailed tutorial on BigDecimal and BigInteger in Java discusses more characteristics and usage scenarios of the two classes.

Finally, the complete source code for this tutorial is available on GitHub.

I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE LESSONS

Leave a Reply

avatar
  Subscribe  
Notify of