Adsense
Popular Posts
- MySQL workbench -"Could not decrypt password cache"
- mod_auth_cas.so error: undefined symbol: SSL_connect
- JavaScript, remove trailing insignificant zeros after toFixed function
- Set Windows path command line
- JavaScript Arrays and Associate Arrays
- Design date and signature box in Latex
- Install APXS in Redhat Linux
- super(props) in React
- jQuery, toggle the display
- PHP function for input sanitizing
Wednesday, February 4, 2015
JavaScript, remove trailing insignificant zeros after toFixed function
In JavaScript, toFixed function is used to convert a number into a string, keeping only two decimals:
For example
var num = 6.76789;
var n = num.toFixed(2);
return 6.77
var num = 6;
var n = num.toFixed(2);
return 6.00
But How to remove trailing insignificant zeros?
We can use Number function or parseFloat function such as
Number(num.toFixed(4));
or
parseFloat(num.toFixed(4));
Labels:
javascript
Subscribe to:
Post Comments (Atom)
This will not work for small numbers (ex: 0.00000012) which JS tends to display in exponential form...
ReplyDelete