Based on http://www.mredkj.com/javascript/nfbasic2.html, following code will result in 5.6e+2.
num = 555.55; result = num.toPrecision(2); // result will equal 5.6e+2
How can I let the output of the result variable be displayed without scientific notation (i.e., e)?
The Scientific format displays a number in exponential notation, replacing part of the number with E+n, in which E (exponent) multiplies the preceding number by 10 to the nth power. For example, a 2-decimal scientific format displays 12345678901 as 1.23E+10, which is 1.23 times 10 to the 10th power.
toFixed(n) provides n length after the decimal point; toPrecision(x) provides x total length.
The toPrecision() method returns a string representing the Number object to the specified precision.
To get a float with reduced precision, you could use toPrecision()
like you do, and then parse the scientific notation with parseFloat(), like so:
result = parseFloat(num.toPrecision(2));
If you do not wish to reduce precision, you could use toFixed() to get the number with a certain number of decimals.
Number((555.55).toPrecision(2))
http://jsfiddle.net/K5GRb/
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