在 Java 中使用 double 保持精度 [英] Retain precision with double in Java

查看:30
本文介绍了在 Java 中使用 double 保持精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class doublePrecision {
    public static void main(String[] args) {

        double total = 0;
        total += 5.6;
        total += 5.8;
        System.out.println(total);
    }
}

上面的代码打印:

11.399999999999

我如何才能将其打印(或能够用作)11.4?

How would I get this to just print (or be able to use it as) 11.4?

推荐答案

正如其他人提到的,您可能想要使用 BigDecimal 类,如果您想精确表示 11.4.

As others have mentioned, you'll probably want to use the BigDecimal class, if you want to have an exact representation of 11.4.

现在,稍微解释一下为什么会发生这种情况:

Now, a little explanation into why this is happening:

Java 中的 floatdouble 原始类型是 浮点数字,其中数字存储为分数和指数的二进制表示.

The float and double primitive types in Java are floating point numbers, where the number is stored as a binary representation of a fraction and a exponent.

更具体地说,诸如 double 类型的双精度浮点值是一个 64 位值,其中:

More specifically, a double-precision floating point value such as the double type is a 64-bit value, where:

  • 1 位表示符号(正或负).
  • 11 位用于指数.
  • 有效数字为 52 位(小数部分为二进制).

这些部分组合起来产生一个值的double表示.

These parts are combined to produce a double representation of a value.

(来源:维基百科:双精度)

有关如何在 Java 中处理浮点值的详细说明,请参阅 第 4.2.3 节:Java 语言规范的浮点类型、格式和值.

For a detailed description of how floating point values are handled in Java, see the Section 4.2.3: Floating-Point Types, Formats, and Values of the Java Language Specification.

bytecharintlong 类型是 定点 数字,它们是数字的精确表示.与定点数不同,浮点数有时(假设大部分时间"是安全的)无法返回数字的精确表示.这就是为什么你最终得到 11.399999999999 作为 5.6 + 5.8 的结果的原因.

The byte, char, int, long types are fixed-point numbers, which are exact representions of numbers. Unlike fixed point numbers, floating point numbers will some times (safe to assume "most of the time") not be able to return an exact representation of a number. This is the reason why you end up with 11.399999999999 as the result of 5.6 + 5.8.

当需要一个精确的值时,例如 1.5 或 150.1005,您需要使用定点类型之一,它能够准确地表示数字.

When requiring a value that is exact, such as 1.5 or 150.1005, you'll want to use one of the fixed-point types, which will be able to represent the number exactly.

正如已经多次提到的,Java 有一个 BigDecimal 类将处理非常大的数字和非常小的数字.

As has been mentioned several times already, Java has a BigDecimal class which will handle very large numbers and very small numbers.

来自 BigDecimal 类的 Java API 参考:

From the Java API Reference for the BigDecimal class:

不可变,任意精度有符号十进制数字.BigDecimal 包含一个未缩放的任意精度整数值和 32 位整数标度.如果零或正,比例是数字右边的位数小数点.如果为负,则数字的未缩放值是乘以十的幂规模的否定.的价值代表的数字BigDecimal 因此是 (unscaledValue× 10^-比例).

Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. 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).

在 Stack Overflow 上有很多关于浮点数及其精度的问题.以下是您可能感兴趣的相关问题列表:

There has been many questions on Stack Overflow relating to the matter of floating point numbers and its precision. Here is a list of related questions that may be of interest:

如果您真的想深入了解浮点数的具体细节,请查看 每位计算机科学家都应该了解的浮点运算知识.

If you really want to get down to the nitty gritty details of floating point numbers, take a look at What Every Computer Scientist Should Know About Floating-Point Arithmetic.

这篇关于在 Java 中使用 double 保持精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆