What is the inclusive range of float and double in Java?

前端 未结 5 557
你的背包
你的背包 2020-11-28 12:22

What is the inclusive range of float and double in Java?

Why are you not recommended to use float or double for anything where precision is critical?

5条回答
  •  迷失自我
    2020-11-28 13:14

    From Primitives Data Types:

    • float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in section 4.2.3 of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.

    • double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in section 4.2.3 of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.

    For the range of values, see the section 4.2.3 Floating-Point Types, Formats, and Values of the JLS.

提交回复
热议问题