Created
April 28, 2018 01:24
-
-
Save ruter/19943646c5c988e72b25f410ff8bf943 to your computer and use it in GitHub Desktop.
用 jQuery 为数字添加或删除千分符
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 为数字添加千分符 | |
function intcomma (num) { | |
if ($.isNumeric(num)) { | |
var DIGIT_PATTERN = /(^|\s)\d+(?=\.?\d*($|\s))/g; | |
var MILLI_PATTERN = /(?=(?!\b)(\d{3})+$)/g; | |
return num.toString().replace(DIGIT_PATTERN, function (m) { | |
return m.replace(MILLI_PATTERN, ','); | |
}); | |
} else { | |
return num; | |
} | |
} | |
// 删除数字千分符 | |
function nonIntcomma (str) { | |
if (!$.isNumeric(str)) { | |
return Number(str.split(',').join('')); | |
} else { | |
return Number(str); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment