toFixed(n) provides n length after the decimal point; toPrecision(x) provides x total length.
In JavaScript, toPrecision() is a Number method that is used to convert a number to a specified precision (rounding the result where necessary) and return its value as a string.
Description. toFixed() returns a string representation of numObj that does not use exponential notation and has exactly digits digits after the decimal place. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length.
The toFixed() and toPrecision() methods round the number to get accurate value in scientific or financial data.
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.
I believe that the former gives you a fixed number of decimal places, whereas the latter gives you a fixed number of significant digits.
Math.PI.toFixed(2); // "3.14"
Math.PI.toPrecision(2); // "3.1"
Furthermore, toPrecision
will yield scientific notation if there are more integer digits in the number than the specified precision.
(Math.PI * 10).toPrecision(2); // "31"
(Math.PI * 100).toPrecision(2); // "3.1e+2"
EDIT: Oh, and if you are new to JavaScript, I can highly recommend the book "JavaScript: The Good Parts" by Douglas Crockford.
Examples speak clearly:
var A = 123.456789;
A.toFixed() // 123
A.toFixed(0) // 123
A.toFixed(1) // 123.5 round up last
A.toFixed(2) // 123.46 round up last
A.toFixed(3) // 123.457 round up last
A.toFixed(4) // 123.4568 round up last
A.toFixed(5) // 123.45679 round up last
A.toFixed(6) // 123.456789
A.toFixed(7) // 123.4567890
A.toFixed(8) // 123.45678900
A.toFixed(9) // 123.456789000
A.toFixed(10) // 123.4567890000
A.toFixed(11) // 123.45678900000
A.toPrecision() // 123.456789
A.toPrecision(0) // --- ERROR ---
A.toPrecision(1) // 1e+2
A.toPrecision(2) // 1.2e+2
A.toPrecision(3) // 123
A.toPrecision(4) // 123.5 round up last
A.toPrecision(5) // 123.46 round up last
A.toPrecision(6) // 123.457 round up last
A.toPrecision(7) // 123.4568 round up last
A.toPrecision(8) // 123.45679 round up last
A.toPrecision(9) // 123.456789
A.toPrecision(10) // 123.4567890
A.toPrecision(11) // 123.45678900
I think this is best answered with an example.
Let's say you have the following data:
var products = [
{
"title": "Really Nice Pen",
"price": 150
},
{
"title": "Golf Shirt",
"price": 49.99
},
{
"title": "My Car",
"price": 1234.56
}
]
You want to display each of these products with the title and formatted price. Let's try using toPrecision
first:
document.write("The price of " + products[0].title + " is $" + products[0].price.toPrecision(5));
The price of Really Nice Pen is $150.00
Looks good, so you might think this will work for the other products as well:
document.write("The price of " + products[1].title + " is $" + products[2].price.toPrecision(5));
document.write("The price of " + products[2].title + " is $" + products[2].price.toPrecision(5));
The price of Golf Shirt is $49.990
The price of My Car is $1234.6
Not so good. We can fix this by changing the number of significant digits for each product, but if we're iterating over the array of products that could be tricky. Let's use toFixed
instead:
document.write("The price of " + products[0].title + " is $" + products[0].price.toFixed(2));
document.write("The price of " + products[1].title + " is $" + products[2].price.toFixed(2));
document.write("The price of " + products[2].title + " is $" + products[2].price.toFixed(2));
The price of Really Nice Pen is $150.00
The price of Golf Shirt is $49.99
The price of My Car is $1234.56
This produces what you expected. There is no guess work involved, and there is no rounding.
Just:
49.99.toFixed(5)
// → "49.99000"
49.99.toPrecision(5)
// → "49.990"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With