Is there any function in Javascript for formatting number and strings ?
I am looking for a way for thousand separator for string or numbers... (Like String.Format In c#)
Is there any function in Javascript for formatting number and strings ?
I am looking for a way for thousand separator for string or numbers... (Like String.Format In c#)
for the reference you can check here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
Attempting to handle this with a single regular expression (without callback) my current ability fails me for lack of a negative look-behind in Javascript... never the less here's another concise alternative that works in most general cases - accounting for any decimal point by ignoring matches where the index of the match appears after the index of a period.
» Verbose regex explanation (regex101.com)
There's a nice jQuery number plugin: https://github.com/teamdf/jquery-number
It allows you to change any number in the format you like, with options for decimal digits and separator characters for decimal and thousand:
You can use it inside input fields directly, which is nicer, using same options like above:
Or you can apply to a whole set of DOM elements using selector:
I use this:
source: link
I did not like any of the answers here, so I created a function that worked for me. Just want to share in case anyone else finds it useful.