Skip to content

Instantly share code, notes, and snippets.

@fjaguero
Created October 11, 2013 09:28
Show Gist options
  • Select an option

  • Save fjaguero/6932045 to your computer and use it in GitHub Desktop.

Select an option

Save fjaguero/6932045 to your computer and use it in GitHub Desktop.
JS Regex: Adds thousands separator to a number.
// Adds the thousands separator
function numberWithThousands(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
}
@scq000

scq000 commented Dec 5, 2017

Copy link
Copy Markdown

Not exactly right, numberWithThousands(12234.3242342) will get "12,234.3,242,342".

@samithafg

Copy link
Copy Markdown

This works just fine. "1234567.89" turns into "1.234.567.89" due to the separator is ".", replacing it with "," turns the first value to "1,234,567.89"

@karakhanyans

Copy link
Copy Markdown

With "." it works fine, result: 123.456.789
With "," it the result is: 1,2,3,4,5,6,789

@etewiah

etewiah commented Aug 30, 2018

Copy link
Copy Markdown

Worth noting that for many cases toLocaleString will work just fine:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

@bogere

bogere commented May 31, 2019

Copy link
Copy Markdown

Worth noting that for many cases toLocaleString will work just fine:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

how do you cater for users who have English UK or another language in their browser

@fjaguero

Copy link
Copy Markdown
Author

Worth noting that for many cases toLocaleString will work just fine:
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

how do you cater for users who have English UK or another language in their browser

If you don't pass a locale parameter, it will get it from the browser.

@bogere

bogere commented May 31, 2019

Copy link
Copy Markdown

Thanks but it has failed to work on my browser...
failed to add the thousand seperators
Google Chrome is up to date
Version 74.0.3729.169 (Official Build) (64-bit)

@fjaguero

Copy link
Copy Markdown
Author

Thanks but it has failed to work on my browser...
failed to add the thousand seperators
Google Chrome is up to date
Version 74.0.3729.169 (Official Build) (64-bit)

Then pass toLocaleString('en-GB')

@bogere

bogere commented May 31, 2019

Copy link
Copy Markdown

i think it might be a reactjs problem for not changing the values. Thank you for the help

@OmarNassar01

OmarNassar01 commented Jun 30, 2020

Copy link
Copy Markdown

formatter={(value) => ` ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')} parser={(value) => value.replace(/\$\s?|(,*)/g, '')}

I'm using this, with the comma works fine but i want to use a dot as a thousand separator and a comma for decimals.
But if i put a dot instead of a comma i get this result 2.0.0.0.0.000 can someone help me?

@fjaguero

fjaguero commented Jul 2, 2020

Copy link
Copy Markdown
Author

@anuar-s

anuar-s commented Feb 7, 2024

Copy link
Copy Markdown

formatter={(value) => ` ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')} parser={(value) => value.replace(/\$\s?|(,*)/g, '')}

I'm using this, with the comma works fine but i want to use a dot as a thousand separator and a comma for decimals. But if i put a dot instead of a comma i get this result 2.0.0.0.0.000 can someone help me?

@OmarNassar01 Hi, did you find solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment