Last active
August 29, 2015 14:02
-
-
Save takunagai/055505b354120939fa4b to your computer and use it in GitHub Desktop.
三桁区切りでカンマを付加
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
/** | |
* 三桁区切りでカンマを付加 | |
* | |
* @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