I'm new to JavaScript and just discovered toFixed()
and toPrecision()
to round numbers. However, I can't figure out what the difference between the two is.
What is the difference between number.toFixed()
and number.toPrecision()
?
toFixed(n)
provides n
length after the decimal point; toPrecision(x)
provides x
total length.
Ref at w3schools: toFixed and toPrecision
EDIT:
I learned a while back that w3schools isn't exactly the best source, but I forgot about this answer until I saw kzh's, uh, "enthusiastic" comment. Here are additional refs from Mozilla Doc Center fortoFixed()
and fortoPrecision()
. Fortunately for all of us, MDC and w3schools agree with each other in this case.
For completeness, I should mention that toFixed()
is equivalent to toFixed(0)
and toPrecision()
just returns the original number with no formatting.