「javascript」数値を3桁区切りで表示するメモ

サンプルコード
function cftFmt(str){
var newStr = “";
var count = 0;

if(str.indexOf(“.")==-1){
for(var i=str.length-1;i>=0;i–){
if(count % 3 == 0 && count != 0){
newStr = str.charAt(i) + “," + newStr;
}else{
newStr = str.charAt(i) + newStr;
}
count++;
}
str = newStr + “.00";
}
else
{
for(var i = str.indexOf(“.")-1;i>=0;i–){
if(count % 3 == 0 && count != 0){
newStr = str.charAt(i) + “," + newStr;
}else{
newStr = str.charAt(i) + newStr;
}
count++;
}
str = newStr + (str + “00").substr((str + “00").indexOf(“."),3);
}
return str;
}

実行結果
cftFmt('13213.24’); //13,213.24

cftFmt('132134.2’);  //132,134.20

cftFmt('132134’);  //132,134.00

cftFmt('132134.236’);  //132,134.23

JavaScript

Posted by arkgame