In my Angular 2 / Typescript project, I am working with a string that contains numeric values such as the ones listed below:
10000
10000.50
-10000
-10000.50
0
I need to insert commas after every thousand mark in these numbers, like so:
10,000
10,000.50
-10,000
-10,000.50
0
What is the most effective way to achieve this?
I have experimented with different solutions but none of them seem to work perfectly.
For instance, using
this.value.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
and this.value.toLocaleString();
do not handle both the commas and decimal points correctly.