Format a double to fixed decimal length

To forestall the next question: if you care about the exact decimals, you probably shouldn’t be using a binary floating-point type like Double.

(There’s no Double which has the exact value of 12345.12345 — just one with the nearest possible binary fraction to it. Functions like println() try to hide that by picking the most likely decimal number, but sooner or later you hit trouble.)

BigDecimal is often a good alternative.

1 Like