Skip to content

Instantly share code, notes, and snippets.

@domino14
Created November 28, 2012 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domino14/4158433 to your computer and use it in GitHub Desktop.
Save domino14/4158433 to your computer and use it in GitHub Desktop.
> formatToFixed = function(value) {
... // turn value into a number if it is not, then
... // turn it into a fixed string. Then finally turn
... // that back into a number.
... tempVal = +((+value).toFixed(6));
... // then
... newValue = tempVal.toString();
... return newValue;
... }
[Function]
> for (var i = 0; i < numbers.length; i++) { console.log(numbers[i], '->', formatToFixed(numbers[i])) }
1e-15 '->' '0'
0.111111111111 '->' '0.111111'
0.1 '->' '0.1'
12435.34314 '->' '12435.34314'
134341.33 '->' '134341.33'
2423.11 '->' '2423.11'
2423.1 '->' '2423.1'
123124123.3 '->' '123124123.3'
0.123456789 '->' '0.123457'
-12.12313123 '->' '-12.123131'
-0.111111111 '->' '-0.111111'
-98.34 '->' '-98.34'
-98.3451 '->' '-98.3451'
undefined
>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment