Skip to content

Instantly share code, notes, and snippets.

@takunagai
Last active August 29, 2015 14:02
Show Gist options
  • Save takunagai/055505b354120939fa4b to your computer and use it in GitHub Desktop.
Save takunagai/055505b354120939fa4b to your computer and use it in GitHub Desktop.
三桁区切りでカンマを付加
/**
* 三桁区切りでカンマを付加
*
* @param {Number or String} 整形したい数値。小数点あり、文字列でもOK
* https://gist.github.com/oreo3/055505b354120939fa4b
*/
var addFigure = function (num) {
num = num + '';//文字列に型変換
if (num.length >= 4){
num = num.replace(/,/g, '');//カンマがあれば除去
while(num !== (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
return num;
}
else {
return num;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment