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()?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
toFixed(n)providesnlength after the decimal point;toPrecision(x)providesxtotal length.Ref at w3schools: toFixed and toPrecision
For completeness, I should mention that
toFixed()is equivalent totoFixed(0)andtoPrecision()just returns the original number with no formatting.And of course, the real source of truth is the JS specification, which in this case is https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.prototype.toprecision