Quantcast
Viewing all articles
Browse latest Browse all 7

Answer by David Tang for Round the value in Javascript

You can convert a string into a number and back into a string to format it as "0.03":

var input = "000.03";var output = (+input).toString(); // "0.03"

To get rid of any leading zeroes (e.g. ".03"), you can do:

var input = "000.03";var output = input.substr(input.indexOf(".")); // ".03"

However, this improperly strips "20.30" to ".30". You can combine the first two methods to get around this:

var input = "000.03";var output = Math.abs(+input) < 1 ?    input.substr(input.indexOf(".")) :    (+"000.03").toString();

Viewing all articles
Browse latest Browse all 7

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>