Last active
August 29, 2015 14:08
-
-
Save think49/c13758815bc0af4b19e3 to your computer and use it in GitHub Desktop.
insert-comma-delimiter.js: 小数or整数文字列に3桁区切りでカンマを挿入します。
This file contains hidden or 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
/** | |
* insert-comma-delimiter.js | |
* Insert commas in a numeric string. | |
* | |
* @version 1.0.1 | |
* @author think49 | |
* @url https://gist.github.com/think49/c13758815bc0af4b19e3 | |
* @license http://www.opensource.org/licenses/mit-license.php (The MIT License) | |
*/ | |
'use strict'; | |
/** | |
* insert comma delimiter | |
* @function | |
* @param {String} numberString Numeric string of decimal or integer. | |
* @return {String} Numeric string of comma-separated. | |
*/ | |
function insertCommaDelimiter (numberString) { | |
return numberString.replace(/(\d+)(\.\d+)?/, function (subString, capture1, capture2) { | |
capture1 = capture1.split(/(?=(?:\d{3})+$)/).join(); | |
return capture2 ? capture1 + capture2.replace(/(\d{3})(?=\d)/, '$1,') : capture1; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment