Quantcast
Channel: Round the value in Javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 7

Answer by KooiInc for Round the value in Javascript

$
0
0

How about:

function showRounded(val) {    var zero = parseInt(val.split('.')[0],10) === 0;    return zero ? val.substring(val.indexOf('.')) : val.replace(/^0+/,'') );}console.log(showRounded('000.03')); //=> ".03"console.log(showRounded('900.03')); //=> "900.03"console.log(showRounded('009.03')); //=> "9.03"

Or adjust Álvaro G. Vicario's solution to get rid of leading zero's into:

String(parseFloat("090.03")).replace(/^0+\./, ".")

Viewing all articles
Browse latest Browse all 7

Trending Articles



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