ARTICLE AD BOX
I'm working on a small frontend utility that needs to convert millimeters to centimeters.
The basic formula is simple cm = mm / 10, but I'm seeing floating-point precision issues in some cases, for example:
12.3 mm -> 1.2299999999999998 cmWhat is a clean and reliable way to handle this in JavaScript?
23k6 gold badges28 silver badges45 bronze badges
New contributor
Isabella is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
5

It turns out that the numbers 12.3 and 1.23 do not exist in the binary floating-point format used by JavaScript and most computers — that's why you're seeing 1.22999. The easiest thing to do is to print with limited precision — say, just 2 or 3 places past the decimal. But you do definitely want to round (not truncate).
2026-01-12 01:40:56 +00:00
Commented 5 hours ago
(12.3).toFixed(18) will output "12.300000000000000711" and (12.3/10).toFixed(18) will output "1.229999999999999982", but (12.3*0.1).toFixed(18) will output "1.230000000000000204". In general, multiplication is better than division.
2026-01-12 03:33:24 +00:00
Commented 3 hours ago
@Wiimm Multiplication is better than division in some circumstances, but not all, and in any case the conclusion doesn't follow from your example! The OP wants to use something like toFixed(2) or toFixed(3), not 18. Using a large number of places is what you do if you want to see the quantization error, but the OP would like to hide it.
2026-01-12 03:53:22 +00:00
Commented 3 hours ago