Add the number spacing with jQuery
source link: https://www.codesd.com/item/add-the-number-spacing-with-jquery.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Add the number spacing with jQuery
I would like to inspect a span element and check for its number format and apply digit grouping to it with jquery
It the number has more than 3 digits, that is for >1000, it must do digit grouping, to separate the thousands from the hundreds, and the hundred thousands from the thousands:
<span>5500000.00</span>
should become
<span>5 500 000.00</span>
Is that possible with jQuery?
There are number of formatting plugins for this task. Or use can use regular expression.
function formatPrice(price) {
return price.reverse().replace(/((?:\d{2})\d)/g, '$1 ').reverse();
}
// Need to extend String prototype for convinience
String.prototype.reverse = function() {
return this.split('').reverse().join('');
}
Tests:
formatPrice('1234.00') // "1 234.00"
formatPrice('12345.00') // "12 345.00"
formatPrice('123456.00') // "123 456.00"
Maybe there is a way to do this without reverting a string?
Related Articles
Add the option dynamically with jquery and update the html source
How to detect the scrolling position with jquery unrelated to scrolling events?
How to add different values for the same CSS property in the same element with jQuery?
Delete the row of a table and add it to another with jQuery
How to initialize and control the web form with jQuery
How to set the data attribute with jQuery on the item that is not yet in DOM?
Phone number format with jquery & regex done
How to add the Splash screen with the progress bar in Webview Android Studio
How to change the button class after calling the asp method with jQuery?
In Excel, add 1 if not the number, or add the number
Delete the dom code if the mobile browser with jQuery
how to change the value of the text box with jQuery?
How to add a dropdown list next to the filter box with Jquery data?
How to add a different style with jquery in the same div name?
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK