Skip to content Skip to sidebar Skip to footer

If Decimal Value, Convert To Two Decimals AND Dot Separated Value To Comma Separated

I currently have values that look like the following: 30 32.5 How can I convert these to have two decimals if there is any decimals present (like the 2nd example), AND have the do

Solution 1:

Try,

var num = 32.5;
num = num.toFixed(2).split('.').join();

DEMO

var num = 32;
num = (num.toString().indexOf('.') > -1) ? num.toFixed(2).toString().split('.').join() : num;

DEMO


Solution 2:

 var num = 32.5;

 num = (num % 1 != 0) ? num.toFixed(2).toString().replace(".", ",") : num;

Demo


Solution 3:

Try this

            var a=52;
            var b=44.4;
            a=Number(a.toFixed(2)).toString();
            b=Number(b.toFixed(2)).toString();
            a=a.split(".").join(",");
            b=b.split(".").join(",");
            console.log(a,b)

Post a Comment for "If Decimal Value, Convert To Two Decimals AND Dot Separated Value To Comma Separated"