2

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 dot delimiter be replaced by a comma?

After the conversion the above numbers would look like this:

30
32,50
Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232
  • Different solutions suggested here: http://stackoverflow.com/questions/7431833/convert-string-with-dot-or-comma-as-decimal-separator-to-number-in-javascript – Peter Jul 03 '14 at 11:31

3 Answers3

3

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

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
1

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)
Unknownman
  • 473
  • 3
  • 9
1
 var num = 32.5;

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

Demo

Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232
KrishnaDhungana
  • 2,604
  • 4
  • 25
  • 37