2

I have a value 2000000 and i want this to be formatted as 2,000,000.00

below is the script i have tried but not able to get the exact output.

function myFunction() {
  var num = 2000000;

  var c = num.toLocaleString()
  var n = num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  //var number=n.
  var number = parseInt(n).toFixed(2);
  document.getElementById("demo").innerHTML = n;
  document.getElementById("demmo").innerHTML = number;
}

This function gives 2,000,000 and 2.00 but it should be 2,000,000.00 help me to get the required result.

31piy
  • 23,323
  • 6
  • 47
  • 67

5 Answers5

4

Use toFixed, then add the , and ensure that there are two 0 at the end:

const addTo = (add, length) => str => str.length >= length ? str : addTo(add, length)(str + add);
const chunk = (sym, length) => str => str.length < length ? str : chunk(sym, length)(str.substr(0, str.length - length)) + sym + str.substr(str.length - length);


const [num, fraction] = 2000000..toFixed(2).split(".");
return chunk(",", 3)(num) + "." + addTo("0", 2)(fraction);
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
4

You could use the NumberFormat object of the ECMAScript Internationalization API:

let num = 2000000;
let l10nEN = new Intl.NumberFormat("en-US", { minimumFractionDigits: 2 });

console.log(l10nEN.format(num));

Or simply use the toLocaleString() method of the number type:

let num = 2000000;

console.log(num.toLocaleString("en-US", { minimumFractionDigits: 2 }));
Krisztián Balla
  • 19,223
  • 13
  • 68
  • 84
2
function myFunction() {
  var num = 2000000.00;

   var c = num.toLocaleString();
   var substr = c.split('.');
   var decimal = substr[1];
   var intVal= substr[0];

   var n = intVal.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
   //var number=n.
   var number = n+ decimal;
   document.getElementById("demo").innerHTML = n;
   document.getElementById("demmo").innerHTML = number;
}
Ramesh
  • 2,297
  • 2
  • 20
  • 42
  • You converted the value to string and added **,**. Thats how it has to be done. Then after you are converting the string to number which only gets 2 as the number which ignores the rest after **,** . You need to add .00 during the string replace. It's like concatenating. `n=n+".00"` – Ramesh Jul 26 '18 at 08:07
  • i got your point but if the value is 2000000.87 then appending .00 will not work right? – Arunkumar Thangavel Jul 26 '18 at 08:09
  • Yeah.In that case you have to separate the decimal and integer at the very beginning. Then do your process adding **,** and then concatenate the decimal which you have already separated to a variable. – Ramesh Jul 26 '18 at 08:12
  • Updated my answer.Please check it out – Ramesh Jul 26 '18 at 08:18
  • thanks. its working after handling the undefined case. – Arunkumar Thangavel Jul 26 '18 at 08:43
  • Happy to here that friend – Ramesh Jul 26 '18 at 08:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/176797/discussion-between-t-arunkumar-and-ramesh). – Arunkumar Thangavel Jul 26 '18 at 11:26
2

Try this

    num.toLocaleString('en', { minimumFractionDigits:2 })
bamse
  • 4,243
  • 18
  • 25
2

You could use Number.prototype.toLocaleString() and Number.prototype.toFixed() to achieve the required result.

DEMO

let value = 2000000;

value = value.toFixed(2).split('.');

console.log(Number(value[0]).toLocaleString('en') + '.' + value[1]);

value= 2000000.87;

value = value.toFixed(2).split('.');

console.log(Number(value[0]).toLocaleString('en') + '.' + value[1]);
Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44