js j数字を三桁ずつコンマで区切る方法
2437 ワード
1
...
2
...
3
...
var numbers = {
one : function(){
var oneval = $(".one_input").val();
$(".one_text").html(parseFloat(oneval).toLocaleString());
},
two : function(){
var twoval = $(".two_input").val();
$(".two_text").html(fun.format_number1(twoval));
},
three : function(){
var threeval = $(".three_input").val();
$(".three_text").html(fun.format_number1(threeval));
}
}
var fun = {
format_number1 : function(n){
var b=parseInt(n).toString();
var len=b.length;
if(len<=3){return b;}
var r=len%3;
return r>0?b.slice(0,r)+","+b.slice(r,len).match(/\d{3}/g).join(","):b.slice(r,len).match(/\d{3}/g).join(",");
},
format_number2 : function(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"; //
console.log(str)
}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);
console.log(str)
}
}
}
転載先:https://www.cnblogs.com/mymission/p/6902186.html