Only add thousand separator before decimal comma

reaper_unique

I have found a regex on stackoverflow to add a ',' or '.' after every third number depending on your language.

(\d)(?=(\d\d\d)+(?!\d))

The problem is that it also happens when we reach the decimal point like with for example:

5487445.46878

The result of that with the following code (and regex) is:

return number.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");

Which results in:

5,487,445.46,878

I'm using this regex when I'm converting a number depending on your language. In Dutch for example a comma is used as a seperator, so there I do the following:

return number.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1.")

Which results in

5.487.445,46.878

These of course need to become:

5,487,445.46878

and

5.487.445,4687

Does anybody know where I need to update the regex to ignore the decimal point?

Martin

You can use your RegEx but split your number first if its has a comma or point in it.

jsFiddle demo

 var input = '5487445.46878';
 var parts = input.split('.');
 var part1 = parts[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
 var part2 = parts[1];

 alert(part1 + '.' + part2);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Display number in Textview with thousand separator and decimal format

Add thousand separator to product quantity in WooCommerce

Thousand separator while typing and decimal (with comma)

Regular expression for thousand separator without decimal point

R: How can I read a CSV file with data.table::fread, that has a comma as decimal and point as thousand separator="."

Specify thousand separator in ssrs without specifying decimal

How to add comma thousand separator in Power BI [Query Editor]

How to read pandas CSV file with comma separator and comma thousand separator

decimal separator in woocommerce to be a comma

How to add thousand separator in type Int and Float?

to_char() problem decimal points and thousand separator

AngularJS: Change decimal separator to comma

How to get Decimal and Thousand separator of toLocaleString() method?

Add comma and zero value after decimal to a string of Number after thousand in Swift

Replace point decimal separator with comma

How to add thousand separator in react native

Cast Varchar to Decimal with Thousand Separator Error

dot instead of comma as thousand separator

Add comma separator to a numbers with 2 decimal points

Html Text box only allow numeric while typing thousand separator and decimal

Comma as decimal separator space as thousand separator C#

Thousand separator with commas and decimal with dots on django template

Regex for custom decimal and thousand separator

add a thousand separator to a paragraph from an input tag

how to add thousand separator to header tag value

Add 2 decimal points & thousand separator to input value Angular

Add thousand separator in Woocommerce email 'Quantity' values

Pandas Formatting with Currency sign, custom thousand separator and custom decimal separator

How to add thousand separator to Textfield?