How to Separate Double into Integer and Decimal Parts – 如何将双数分离成整数和小数部分

最后修改: 2018年 9月 21日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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

在本教程中,我们将探讨各种方法来分离Java中浮点类型的整数和小数部分,即floatdouble

2. Issues with Floating-Points Types

2.浮动点类型的问题

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

3.第一种方法 分割字符串

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

首先,让我们把小数点转换成String等值。然后我们可以在小数点索引处分割String

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

4.第二种方法 使用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.

Java中的BigDecimal类为其用户提供了对舍入行为的完全控制。这个类还提供了算术、比例操作、四舍五入、比较、散列和格式转换等操作。

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

让我们使用BigDecimal来获取一个浮点数的整数和小数部分。

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.

正如我们在上面看到的,输出结果与预期一致。我们还可以借助BigDecimal类中提供的方法进行算术运算。

5. Conclusion

5.总结

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.

在这篇文章中,我们讨论了分离浮点类型的整数和小数部分的各种方法。我们还讨论了使用BigDecimal进行浮点计算的好处。

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

此外,我们关于Java中的BigDecimal和BigInteger的详细教程讨论了这两个类的更多特点和使用场景。

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

最后,本教程的完整源代码可在GitHub上获得。