0

there is a way to add a comma to the thousand digits in numbers and also delete the point and the numbers after the point ? in my case i try :

{TOTAL_COSTS_PLAN.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}

and it gives me only the number with no comma inside and I don't understand why.

for example : 5,432.1 should be 5,432 and 567,892.34 should be 567,892 but in my case it doesn't work.

Yevhen Horbunkov
  • 14,965
  • 3
  • 20
  • 42
  • Does this answer your question? [How to print a number with commas as thousands separators in JavaScript](https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript) – Yevhen Horbunkov Feb 13 '20 at 13:01
  • i need to add the commas and delete the dots and the numbers after the dots at the same time like this --->> for example : 5,432.1 should be 5,432 and 567,892.34 should be 567,892 but in my case it doesn't work. –  Feb 13 '20 at 13:05
  • You're mixing subjects again - those are 2 separate issues, [you got](https://stackoverflow.com/questions/60204969/is-there-way-to-add-comma-to-the-thousand-digits-in-numbers) plenty of solutions for trimming fractional part, post referenced above will help you out with formatting – Yevhen Horbunkov Feb 13 '20 at 13:06
  • To help you combine the two: `{``${TOTAL_COSTS_PLAN|0}``.replace(/\B(?=(\d{3})+(?!\d))/g, ",")}` (just use single tick mark instead of double (couldn't manage SO code formatting issue). – Yevhen Horbunkov Feb 13 '20 at 13:12

2 Answers2

1

It is not working with toFixed() because its return type is string,

But it works with Math.round or any other solution giving back number.

let TOTAL = 654321.45;

// 654,321 for me but it may be different for you
console.log(Math.round(TOTAL).toLocaleString()); 

// 654,321 is should be same for everyone because the fixed locale
console.log(Math.round(TOTAL).toLocaleString('en')); 
Peter Ambruzs
  • 7,763
  • 3
  • 30
  • 36
  • Your locale may differ, consider adding a locale parameter to the toLocaleString. I updated my example. Try pressing the Run code button – Peter Ambruzs Feb 13 '20 at 12:06
  • in my case i have number like that : 654321.45 so i need to delete the point and the digits after the point and also i need to add commas to the thousand digits but it dosnt work –  Feb 13 '20 at 12:11
  • I updated my answer to 654321.45 it works for me if I run it with 'Run code snippet' button – Peter Ambruzs Feb 13 '20 at 12:15
  • What is it writing out at the bottom of the window for you? – Peter Ambruzs Feb 13 '20 at 12:22
  • this work : '54343.51'.replace(/\B(?=(\d{3})+(?!\d))/g, ","); it show the comma but how can i delete the point and the numbers after it like this : 54343.51 --->>54343 –  Feb 13 '20 at 12:23
  • That is the next answer, not mine. Ask your question there. – Peter Ambruzs Feb 13 '20 at 12:26
0

Live Test: https://runkit.com/embed/2j6np56a8dhp

'54321'.replace(/\B(?=(\d{3})+(?!\d))/g, ",");

Update: to cater your case in the comment, use following snippet. BTW: this is off-topic of your OP, :) https://runkit.com/embed/lnzcxixzhamh

Math.floor(543435.1).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
David
  • 15,894
  • 22
  • 55
  • 66
  • it show the comma but how can i delete the point and the numbers after it like this : 5,434,35.1 --->>5,434,35 –  Feb 13 '20 at 12:28