2016-11-09 2 views
0

次のコードがあります。テキスト入力フィールドでは、どのようにカンマが数字に適切に追加されるようにすることができますか?たとえば、「1000」と言うのではなく、「1,000」と言います。別の例として、「1000000」と入力すると「1,000,000」となります。残念ながらJSは私の強みではありません。 ありがとうございます!私はカンマが必要コード内のカンマnフィールドと数字を区切ります

function priceCalculation(a){ 
      if(a <= 10000){ 
      return 0.00099; 
      }else if(a >= 10001 && a <= 25000){ 
      return 0.00095; 
      }else if(a >= 25001 && a <= 50000){ 
      return 0.00089; 
      }else if(a >= 50001 && a <= 100000){ 
      return 0.00075; 
      }else{ 
      return 0.00075; 
      } 
     } 

     $('#likecount').keyup(function(){ 
      var price = priceCalculation($(this).val()); 
      console.log(price) 
      var total = $(this).val() * price; 
      $('#output').text(total.toFixed(2)); 
     }); 



<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
     <input type="text" id="likecount" /> 
     <p style="padding: 20px 0px; font-weight: bold; color: #222; ">Your Cost: <b><span> $</span><span id="output" style="color: #004f04;"></span></b></p> 

スクリーンショット: enter image description here

+0

の可能性のある重複:私は必要http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript – AndreFeijo

+0

@AndreFeijoいや、原因フィールドの解答 – Morgari

答えて

1

更新されたスクリプトを:あなたはそれが好き行うことができます

function priceCalculation(a){ 
 
    if(a <= 10000){ 
 
     return 0.00099; 
 
    }else if(a >= 10001 && a <= 25000){ 
 
     return 0.00095; 
 
    }else if(a >= 25001 && a <= 50000){ 
 
     return 0.00089; 
 
    }else if(a >= 50001 && a <= 100000){ 
 
     return 0.00075; 
 
    }else{ 
 
     return 0.00075; 
 
    } 
 
} 
 

 
// number format set to en-US e.g (from 1500 to 1,500) 
 
var numFormat = new Intl.NumberFormat("en-US"); 
 

 
$('#likecount').keyup(function(e){ 
 
    // if a '.' is pressed 
 
\t if($(this).val().endsWith('.')) { 
 
    \t return; 
 
    } 
 

 
    // if input value is empty then assign '0' else the original value 
 
    var inputVal = $(this).val() === ''?'0':$(this).val(); 
 
    
 
    // replace the ',' to '' so we won't get a NaN result 
 
    // on arithmetic operations 
 
    inputVal = parseFloat(inputVal.replace(/[$|,]/g, '')); 
 
    var price = priceCalculation(parseInt(inputVal)); 
 
    var total = inputVal * price; 
 
    var formatted = numFormat.format(inputVal) // set format to input 
 
    $(this).val(formatted); // display the formatted input back 
 
    $('#output').text(numFormat.format(total)); // display the total price 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" id="likecount" /> 
 
     <p style="padding: 20px 0px; font-weight: bold; color: #222; ">Your Cost: <b><span> $</span><span id="output" style="color: #004f04;"></span></b></p>

+0

すばらしい解決策!しかし、私は入力フィールドの区切り記号が必要です。私の場合、どこにコードを入力すればいいですか(スクリーンショットのような入力フィールドのカンマ)を教えてください。事前にありがとう – Morgari

+1

ああ...まだバグを待ちます – four

+1

@Morgariデモを再確認 – four

1

あなたは、関数のtoLocaleString()、それが望む最終的な数はあなたのようになりますことを確認しますを使用することができます。

console.log((1000000).toLocaleString()) 
 
console.log((123456.321).toLocaleString()) 
 
console.log((123.3212).toLocaleString()) 
 
console.log((1234.56).toLocaleString())

+0

ありがとうございました。残念ながら、私のJSの知識は非常に悪いです。このコードはどこに入力すればよいですか? – Morgari

+1

'$( '#output')。text(total.toLocaleString())' – Dekel

+0

残念ながら、何も変わっておらず、フィールドの数字もカンマなしです。 – Morgari

1

使用可能:

var profits=2489.8237 profits.toFixed(3) //returns 2489.824 (round up) profits.toFixed(2) //returns 2489.82 profits.toFixed(7) //returns 2489.8237000 (padding) 

次に、 '$'の記号を追加できます。

あなたが必要な場合は 'を、' 千のためにあなたが使用することができます。

Number.prototype.formatMoney = function(c, d, t){ var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "." : d, t = t == undefined ? "," : t, s = n < 0 ? "-" : "", i = String(parseInt(n = Math.abs(Number(n) || 0).toFixed(c))), j = (j = i.length) > 3 ? j % 3 : 0; return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ""); }; 

し、それを使用する: '' あなたは常に使用するつもりなら

(123456789.12345).formatMoney(2, '.', ','); 

と '、'、あなたはあなたのメソッド呼び出しからそれらを離れることができ、メソッドはあなたのためにそれらをデフォルト設定します。

(123456789.12345).formatMoney(2); 

あなたの文化は二つのシンボル(すなわちヨーロッパ人)が反転している場合は、単にformatMoneymethodに以下の2行に貼り付けます。書式設定ECMAScript 6の数(see here)を用いた

d = d == undefined ? "," : d, t = t == undefined ? "." : t, 
+0

ありがとうございました。@ javarbtech。残念ながら、私のJSの知識は非常に悪いです。このコードはどこに入力すればよいですか? – Morgari

+0

NP ...それがあなたを助けるならば同意してください – jafarbtech

関連する問題