views:

55

answers:

2

Have I missed a standard API call that removes trailing insignificant zeros from a number?

Ex.

var x = 1.234000 // to become 1.234;
var y = 1.234001; // stays 1.234001

Number.toFixed() and Number.toPrecision() are not quite what I'm looking for.

A: 

How about this. Should take away any number of zeros as long as they are at the end of the string.

var str = '1.234000';
str = str.replace(/0*$/, '');
Matti
+1  A: 

If you convert it to a string it will not display any trailing zeros, which aren't stored in the variable in the first place since it was created as a Number, not a String.

var n = 1.245000
var noZeroes = n.toString() // "1.245" 
CD Sanchez
I was about to post some code to strip the zeros but Daniel's solution seems to work. It even works for Strings such as "1.2345000". ("1.2345000" * 1).toString(); // becomes 1.2345
Steven