Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Float and double datatype in Java

The float data type is a single-precision 32-bit IEEE 754 floating point and the double data type is a double-precision 64-bit IEEE 754 floating point.

What does it mean? And when should I use float instead of double or vice-versa?

Answer*

Draft saved
Draft discarded
Cancel
5
  • float give you 8 digits precision. The result of System.out.println(Float.parseFloat("0.123456789")); -> 0.12345679 Commented Oct 5, 2024 at 6:35
  • 1
    @gshock try also System.out.println(Float.parseFloat("0.987654321")); which gives you 0.9876543 Commented Oct 10, 2024 at 13:05
  • 1
    Ok. I found an article that explained it very well arshadsuraj.medium.com/…. Thanks ✌️ Commented Oct 10, 2024 at 21:26
  • This precision is strongly depends on how many mathematical operations (like addition) you execute over numbers (float or double). With each operation there is some error (in mathematical sense). These errors accumulate. After like 100.000 additions for float the actual precision is like 3 digits and for long it's like 8 digits. Commented Jan 21, 2025 at 15:50
  • @KrzysztofTomaszewski don't mix up precision and accuracy. Rounding errors influence the accuracy, You can loose almost all accuracy even with a single operation if you subtract two nearly equal quantities. Commented Jan 22, 2025 at 16:54

lang-java

AltStyle によって変換されたページ (->オリジナル) /