2017-02-15 5 views
-2

javascriptでコンマ区切りの値の値を作成するには?私は、テキストボックスに123000.00を提供する場合 たとえば、結果は1,23,000.00コンマで区切られた10進数のコードスニペットjavascript

+0

[どのようにJavaScriptでお金のようにすることができますが、I形式番号?](// stackoverflowのを試してみました。 com/q/149055) – Tushar

+0

[JavaScriptの通貨書式設定](// stackoverflow.com/q/14467433) – Tushar

+0

123000.00の結果は123,000.00ではありませんか?そうでない場合は、どのシステムでコンマを挿入しますか? –

答えて

0

する必要があります私は、以下の機能

function formatWithCommasAndDecimal(tempInputString,setvalue,decimalPointLen) 
{ 

var inputString = ""; 
    var beforeDecimal; 
    var afterDecimal = "00"; 
    var newBeforeDecimal = ""; 
    var commaCounter = 0; 
    if(tempInputString=="") 
    { 
     if(decimalPointLen=="8") 
     { 
      document.getElementById(setvalue).value="0.00000000"; 
     } 
     else 
     { 
      document.getElementById(setvalue).value="0.00"; 
     }  
     return false; 
    } 
    if(tempInputString!="0.00" || tempInputString!=" ") 
    { 
    if(tempInputString.indexOf(",")!='-1') 
     { 
      //Just remove all the commas 
      var len = tempInputString.length 
      for(i=0;i<len;i++) 
      { 
       if(tempInputString.charAt(i) != ",") 
       { 
        inputString = inputString + tempInputString.charAt(i); 
       } 
      } 
     } 
     else 
     { 
      tempInputString=parseFloat(tempInputString).toFixed(decimalPointLen); 
      inputString = tempInputString; 
     } 

     var tempArr = inputString.split("."); 

     //Format the portion before the decimal 
     beforeDecimal = tempArr[0]; 
     for(i = beforeDecimal.length - 1;i>=0;i--) 
     { 
      newBeforeDecimal = newBeforeDecimal + beforeDecimal.charAt(i); 
      commaCounter++; 
      if(commaCounter % 3 == 0) 
      { 
       //Checking this so that the comma is not appended at the end 
       if(i > 0) 
       { 
        newBeforeDecimal = newBeforeDecimal + ","; 
        commaCounter = 0; 
       } 
      } 
     } 

     beforeDecimal = ""; 
     for(i=newBeforeDecimal.length-1;i>=0;i--) 
     { 
      beforeDecimal = beforeDecimal + newBeforeDecimal.charAt(i); 
     } 

     //Format the portion after the decimal (if any is there) 
     if(tempArr.length != 1) 
     { 
      afterDecimal = tempArr[1]; 
     } 
     var finalString = beforeDecimal + "." + afterDecimal; 
     document.getElementById(setvalue).value=finalString; 
    } 
    return finalString; 
} 
関連する問題