Difference between toFixed() and toPrecision()?

toFixed(n) provides n length after the decimal point; toPrecision(x) provides x total length.

Ref at w3schools: toFixed and toPrecision

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 for toFixed() and for toPrecision(). 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.

Leave a Comment