js金額の数値フォーマットを実現するコード(コンマを減らす処理)

5920 ワード

例1は、数字111111を11,111,111.00にして、2桁の小数を保持する.
 
  


<br>function outputmoney(number) { <br>number = number.replace(/\,/g, ""); <br>if(isNaN(number) || number == "")return ""; <br>number = Math.round(number * 100) / 100; <br>    if (number < 0) <br>        return '-' + outputdollars(Math.floor(Math.abs(number) - 0) + '') + outputcents(Math.abs(number) - 0); <br>    else <br>        return outputdollars(Math.floor(number - 0) + '') + outputcents(number - 0); <br>} <br>// <br>function outputdollars(number) { <br>    if (number.length <= 3) <br>        return (number == '' ? '0' : number); <br>    else { <br>        var mod = number.length % 3; <br>        var output = (mod == 0 ? '' : (number.substring(0, mod))); <br>        for (i = 0; i < Math.floor(number.length / 3); i++) { <br>            if ((mod == 0) && (i == 0)) <br>                output += number.substring(mod + 3 * i, mod + 3 * i + 3); <br>            else <br>                output += ',' + number.substring(mod + 3 * i, mod + 3 * i + 3); <br>        } <br>        return (output); <br>    } <br>} <br>function outputcents(amount) { <br>    amount = Math.round(((amount) - Math.floor(amount)) * 100); <br>    return (amount < 10 ? '.0' + amount : '.' + amount); <br>} <br>





オンライン実行:
Functuputmoney(number){number=number.replace(/\,/g,");if(isNaN(number)==""";number=Math. round(number*100)/100(mber+mber+mber+mber+100)(((=fffftttttber+mber+mber+mber+mber========="))))))"""")""""""")"""""";number=";number=Manumber=Manumber="";number=Mathm')+ outputcents(Math.abs(number)-0);else return outputdollars(Math.floor(number-0)+')+outputcents(number-0)///フォーマット金額function outputdollars(number){i i if((number.length<= 3)return(number=='''?'0':number);else{var mod=number.length%3;vaoutppput==========="""""""""""""""""""""""""""""""""""""""""""""""""""""((((=number=number=number=number=number=number//3);i+){if((mod==0)&(i==0)output+=number.substring(mod+3*i,mod+3*i+3);else outputput+='、'+number.substring(mod+3*i,mod+3*i+3);'returnrn(output))'function outputputcents(amount){amount=Maount=Mathround=====Mathround'ffmmurm'm'm'ffrom'm''''''''''(fffffram'm'from'''''''''''''''''''''''''''''''''''''';
[Ctrl+A全選注:外部Jsを導入するにはリフレッシュが必要です.]
例2において、数字111111を11,111,111---にし、11,111,111を111111---整数にする. 
 
  




js ---www.jb51.net
<br>function tran(id) <br>{ <br>  var v, j, sj, rv = ""; <br>  v = id.value.replace(/,/g,"").split("."); <br>  j = v[0].length % 3; <br>  sj = v[0].substr(j).toString(); <br>  for (var i = 0; i < sj.length; i++) <br>  { <br>    rv = (i % 3 == 0) ? rv + "," + sj.substr(i, 1): rv + sj.substr(i, 1); <br>  } <br>  var rvalue = (v[1] == undefined) ? v[0].substr(0, j) + rv: v[0].substr(0, j) + rv + "." + v[1]; <br>  if (rvalue.charCodeAt(0) == 44) <br>  { <br>    rvalue = rvalue.substr(1); <br>  } <br>  id.value = rvalue; <br>} <br>function tran2(id) <br>{ <br>  var v; <br>  v = id.value.replace(/,/g,""); <br>  alert(v); <br>} <br>








テストの実行:
jsフォーマット金額のコード---www.jb 51.net function tran(id){var v,j,sj,rv=""";v=id=value.replacce(/,g,").split(");j=v[0].length%3;sj=v=[0].substr===="(string======"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""j.substr(i,1):rv+sj.substr(i,1);}var rvalue=(v[1]==undefined)?v[0].substr(0,j)+rv:v[0].substr(0,j)+rv+“.”+v[1];if(rvalue.chaharCodeAt(0)==44){rvalue=rvalue.substr(1);id.value=rvalue;;functiontron 2(id)==================================================================================================""""""""12px;->
[Ctrl+A全選注:外部Jsを導入するにはリフレッシュが必要です.]