1. Overview
1.概述
Double and float are data types that represent decimal numbers in Java. They both differ from each other when handling decimals.
Double 和 float 是 Java 中表示十进制数的数据类型。在处理小数时,它们彼此不同。
In this tutorial, we’ll discuss double and float and learn how to convert them into each other.
在本教程中,我们将讨论 double 和 float 并学习如何将它们相互转换。
2. What Is a Double and Float in Java
2.什么是 Java 中的 Double 和 Float
float is a 32-bit single-precision floating type and can store around 7 decimal digits. This data type has an advantage when there is a need to save memory. However, using float for currencies or high-precision data calculation is not advisable.
float 是一种 32 位单精度浮动类型,可存储约 7 位小数。但是,使用 float 进行货币或高精度数据计算并不可取。
When declaring float variables for primitive datatypes, we could use the float keyword:
在为原始数据类型声明 float 变量时,我们可以使用 float 关键字:
float vatRate = 14.432511f;
For wrapper classes, we should use the Float class:
对于封装类,我们应该使用 Float 类:
Float localTaxRate = 20.12434f;
Since we are dealing with float, variables should have an “f” suffix at the end. Otherwise, the compiler will consider it as a double and will complain.
由于我们处理的是 float 变量,因此变量末尾应带有后缀 “f”。否则,编译器会将其视为 double 并发出抱怨。
On the other hand, double is a 64-bit single-precision floating type. This data type can store more decimal digits than float at approximately 17 decimal places. It is a common choice for decimal calculations because of its greater accuracy. Like float, double is also not recommended for currency calculation.
另一方面,double 是一种 64 位单精度浮动类型。这种数据类型可以存储比 float 更多的小数位数,大约 17 位小数位数。由于其精度更高,因此是小数计算的常用选择。与 float 一样,double 也不推荐用于货币计算。
Below is the illustration of how to declare a double by using the double keyword:
下面是如何使用 double 关键字声明 double 的示例:
double shootingAverage = 56.00000000000001;
Another way to declare a double is through a Double wrapper class:
声明 double 的另一种方法是通过 Double 封装类:
Double assistAverage = 81.123000000045;
When deciding which data type to use, between double and float, it is essential to consider the application’s technical needs and business requirements.
在决定使用 double 和 float 之间的哪种数据类型时,必须考虑应用程序的技术需求和业务要求。
3. Conversions Between Double and Float
3.双倍和浮点之间的转换
It is a common technical requirement to make conversions between Double and Float. However, we should be cautious in converting them as we may lose some decimal precision. Subsequently, this may lead to unexpected behavior in our application.
在 Double 和 Float 之间进行转换是一项常见的技术要求。但是,在转换时我们应该谨慎,因为我们可能会损失一些小数精度。随后,这可能会在我们的应用程序中导致意想不到的行为。
3.1. Converting Double to Float
3.1.将双倍值转换为浮点数
Let’s demonstrate how to convert a variable through casting:
让我们来演示如何通过转换来转换变量:
double interestRatesYearly = 13.333333333333333;
float interest = (float) interestRatesYearly;
System.out.println(interest); //13.333333
Assert.assertEquals(13.333333f, interest, 0.000001f);
Since we performed a casting from double to float, our new float value lost some decimal places compared to its original value.
To test this conversion with an assertEquals(), we’re using a delta parameter with a value of 0.000001 which is sufficient for the converted value of 13.333333.
由于我们执行了从 double 到 float 的转换,因此新的 float 值与原始值相比少了一些小数位数。
为了使用 assertEquals() 测试这种转换,我们使用了一个值为 0.000001 的 delta 参数,这个值足以满足 13.333333 的转换值。
Another way to make a conversion from double to float is through wrapper classes:
将 double 转换为 float 的另一种方法是通过封装类:
Double monthlyRates = 2.111111111111111;
float rates = monthlyRates.floatValue();
System.out.println(rates); //2.1111112
Assert.assertEquals(2.1111111f, rates, 0.0000001f);
In the above example, the monthlyRates object invoked a method called floatValue() that returned a float.
Similarly to our first conversion example, we use a value of 0.0000001 for our delta.
与第一个转换示例类似,我们使用 0.0000001 作为 delta 值。
3.2. Converting Float to Double
3.2.将浮点数转换为双倍数
Next, let’s show how to convert float to double data types:
接下来,让我们展示如何将 float 转换为 double 数据类型:
float gradeAverage =2.05f;
double average = gradeAverage;
System.out.println(average); //2.049999952316284
Assert.assertEquals(2.05, average, 0.01);
We noticed that the conversion to double came out different than what we were expecting. The issue lies in the binary representation of the Floating-Point numbers.
我们注意到,转换为 double 的结果与我们预期的不同。问题在于 浮点数的二进制表示。
A different way of converting float to double is by using the Double class via doubleValue() method:
将 float 转换为 double 的另一种方法是通过 doubleValue() 方法使用 Double 类:
Float monthlyRates = 2.1111111f;
Double rates = monthlyRates.doubleValue();
System.out.println(rates); //2.1111111640930176
Assert.assertEquals(2.11111111, rates, 0.0000001);
Like our previous tests from the previous section, we have used delta parameters in our assertEquals() for our unit tests for this section.
与上一节的测试一样,我们在本节的单元测试 assertEquals() 中使用了 delta 参数。
4. Conclusion
4.结论
In this article, we’ve discussed float and double data types. Each data type has its characteristics and its own precision size. A double data type has more decimal places than a float. However, the float data type has its own advantage regarding memory usage, while double has its advantages in accuracy.
在本文中,我们讨论了 float 和 double 数据类型。每种数据类型都有自己的特点和精度大小。double 数据类型的小数位数比 float 多。不过,float 数据类型在内存使用方面有自己的优势,而 double 则在精度方面有自己的优势。
All code samples used in the article are available over on GitHub.
文章中使用的所有代码示例均可在 GitHub 上获取。