2016-11-10 10 views
0

に追加します。問題があります。私のJS知識&の経験は非常に悪いので、私はあなたの助けなしにそれを解決することはできません。私は単純な電卓のための次のコードを持っています:これはいくつかのコードを追加したい:私の計算機スクリプトの値を切り上げて、カンマ区切りを値の

電卓に10010などの数字を入力すると、私は$ 7.508を返す。どのように私はそれが期間を過ぎて常に2桁があり、それを切り上げることができますか?この場合、$ 7.508の代わりに$ 7.51が表示された方が良いでしょう。私が4000に入ったら "$ 3"が表示されますが、 "$ 3.00"と言えますか? ありがとうございます!

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<input placeholder="How many likes?" style="height: 50px;width: 360px;color: #222;border-radius: 5px;border: 1px #85c9e3 solid;font-size: 18px;" type="text" id="likecount" /> 
 
<p style="font-family: 'Montserrat', sans-serif; padding: 20px 0px; font-weight: bold; color: #222; ">Pricing: <b><span style="color: #004f04;"> $</span><span id="output" style="color: #004f04;"></span></b></p> 
 

 
<script type="text/javascript">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(); 
 
    
 
    inputVal = parseFloat(inputVal.replace(/[$|,]/g, '')); 
 
    var price = priceCalculation($(this).val()); 
 
    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>

+2

[常に小数点以下2桁を表示する形式番号](http://stackoverflow.com/questions/6134039/format-number-to-always-show-2-の可能性のある重複小数点以下桁) – Keno

答えて

2

total.toFixed(2)を追加してtotalを小数点以下2桁にすることができます。私はあなたのためにそれを追加しました。

var total = (inputVal * price).toFixed(2);読むよりおよそ.toFixed at MDN

DEMO以下でそれをチェックしてください。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<input placeholder="How many likes?" style="height: 50px;width: 360px;color: #222;border-radius: 5px;border: 1px #85c9e3 solid;font-size: 18px;" type="text" id="likecount" /> 
 
<p style="font-family: 'Montserrat', sans-serif; padding: 20px 0px; font-weight: bold; color: #222; ">Pricing: <b><span style="color: #004f04;"> $</span><span id="output" style="color: #004f04;"></span></b></p> 
 

 
<script type="text/javascript">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(); 
 
    
 
    inputVal = parseFloat(inputVal.replace(/[$|,]/g, '')); 
 
    var price = priceCalculation($(this).val()); 
 
    var total = (inputVal * price); 
 
    total = (Math.round(total * 100)/100).toFixed(2); 
 
    var formatted = numFormat.format(inputVal) // set format to input 
 
    $(this).val(formatted); // display the formatted input back 
 
    $('#output').text((total)); // display the total price 
 
}); 
 

 
</script>

+0

それは私にとって素晴らしい作品です!切り上げ部分は完全に完璧です。あなたは、この投稿の私の2番目の問題で私を助けてくれますか?私が4000に入ったら "$ 3"が表示されますが、 "$ 3.00"と言えますか? – Morgari

+0

さて、私はそれをチェックアウトさせてください。 – Abk

+1

あなたの 'priceCalculation'関数によれば、' 4000'は '0.00099'になり、' total'は '0.00099 * 4000 = 3.96'になります。 – Abk

1

Intl.NumberFormatあなたのスタイルに影響を与えることを可能にするオプションパラメータを受け付けます。

あなたの例から、あなたが使用できるように、それはそうです:これはまた、(例えば、ここでのように、ドル記号)通貨記号を追加すること

var numFormat = new Intl.NumberFormat("en-US", { style: 'currency', currency: 'USD' }); 

注意。あなたは本当にただ丸めを制御したい場合は、代わりにminimumFractionDigitsmaximumFractionDigitsを設定することがあります。

var numFormat = new Intl.NumberFormat('en-US', { style: 'decimal', minimumFractionDigits: 2, maximumFractionDigits: 2 }); 
1

重要行:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<input placeholder="How many likes?" style="height: 50px;width: 360px;color: #222;border-radius: 5px;border: 1px #85c9e3 solid;font-size: 18px;" type="text" id="likecount" /> 
 
<p style="font-family: 'Montserrat', sans-serif; padding: 20px 0px; font-weight: bold; color: #222; ">Pricing: <b><span style="color: #004f04;"> $</span><span id="output" style="color: #004f04;"></span></b></p> 
 

 
<script type="text/javascript">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(); 
 
    
 
    inputVal = parseFloat(inputVal.replace(/[$|,]/g, '')); 
 
    var price = priceCalculation($(this).val()); 
 
    var total = (inputVal * price); 
 
    total = (Math.round(total * 100)/100).toFixed(2); 
 
    var formatted = numFormat.format(inputVal) // set format to input 
 
    $(this).val(formatted); // display the formatted input back 
 
    $('#output').text(total); // display the total price 
 
}); 
 

 
</script>

var total = (inputVal * price).toFixed(2); 

Working Demo

コードの下に試してみてください

+1

@Morgariは、計算ごとに4000が$ 3.96になります。あなたのコードは$ 3を表示しません。 – Vikrant

+0

それは私にとって素晴らしい作品です!切り上げ部分は完全に完璧です。しかし、2番目のポストパートの場合40,000と入力すると「30ドル」と表示されますが、「30.00ドル」と言うことはできますか? – Morgari

+0

残念ながら値段はまだ30(ノート30.00)です。 – Morgari

関連する問題