Adsense
Popular Posts
- Code update from PHP 7.4 to PHP 8.1 - PhpSpreadsheet
- MySQL workbench -"Could not decrypt password cache"
- Code update from PHP 7.4 to PHP 8.1 - Worksheet/Iterator.php
- Rendering HTML tags inside textarea
- axios handle blob type data
- Unix Utils and wget in Windows
- Setup vi syntax for PHP
- increase mysql query speed
- git svn conversion
- EXCEL return to the next line in the same cell
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