Skip to content

Instantly share code, notes, and snippets.

@think49
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save think49/c13758815bc0af4b19e3 to your computer and use it in GitHub Desktop.
Save think49/c13758815bc0af4b19e3 to your computer and use it in GitHub Desktop.
insert-comma-delimiter.js: 小数or整数文字列に3桁区切りでカンマを挿入します。
/**
* 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