Try using this:
function roundAfterZeros(number,places){
var matches=number.toString().match(/\.0*/);
if(!matches)return number.toString();
return number.toFixed(matches[0].length-1+places);
}
Here's an explanation
var matches = number.toString().match(/\.0*/)
checks for zeros (0
) after the point (.
).
if(!matches)return number.toFixed(places);
if there is no point (.
), it must be a whole number, so we just return it (as a string for consistency).
return number.toFixed(matches[0].length-1+places);
if it is a decimal, we will round it off to the nearest digits after the zeros (0
).
Then run it like roundAfterZeros(0.000083423342,2)
:
0.000083423342 to "0.000083"
1.0342345 to "1.034"
1 to "1"
0.5 to "0.50"
-300 to "-300"