1

私のコードは正常に動作しますが、#chance に 60.1、60.2、60.3 などの 10 進数が入力されると、利益と支払いが台無しになります。

例: チャンスには 60%、ベットには 1 を入力します。支払いには 1.65、利益には 0.65 を返します。それはすべて正しいです。

しかし、60.1 を入力すると、16.5 (間違った小数) と 15.5 の利益が返されます。16.5 は簡単な修正のように思えますが、修正方法はわかりませんが、利益のために 15.5 を返す理由がわかりません。支払いを修正すれば、利益の問題が修正されるのではないかと考えました。

どうしたの?

ありがとう。

<script>
    $(document).ready(function(){

        function updateValues() {
            // Grab all the value just incase they're needed.
            var chance = $('#chance').val();
            var bet = $('#bet').val();
            var pay = $('#pay').val();
            var profit = $('#profit').val();

            // Calculate the new payout.
            var remainder = 101 - chance;
            pay = Math.floor((992/parseFloat((chance+0.5))) *100)/100;


            // Calculate the new profit.
            profit = bet*pay-bet;
                            profit = profit.toFixed(6);


            // Set the new input values.
            $('#chance').val(chance);
            $('#bet').val(bet);
            $('#pay').val(pay);
            $('#profit').val(profit);
        }


        parseInt($('#chance').keyup(updateValues));
        parseInt($('#bet').keyup(updateValues));
        parseInt($('#pay').keyup(updateValues));
        parseInt($('#profit').keyup(updateValues));


    });
</script>
4

3 に答える 3

1

に変更parseFloat((chance+0.5))(parseFloat(chance)+0.5)ます。

実際、なぜそれが で動作しているのかわかりません60chanceは、テキスト フィールドの値として、文字列:"60"です。文字列は加算せず、連結します: "60" + 0.5is "600.5"、 と同じ"60" + "0.5"です。

于 2013-06-27T00:49:23.337 に答える
0

次のようなことを試してください:

$(document).ready(function(){
  function updateValues(){
    var chance = $('#chance').val();
    var bet = $('#bet').val();
    var pay = $('#pay').val();
    var profit = $('#profit').val();
    pay = ((992/Math.floor(+chance+0.5))/10).toFixed(2);
    profit = (bet*pay-bet).toFixed(6);
    $('#chance').val(chance);
    $('#bet').val(bet);
    $('#pay').val(pay);
    $('#profit').val(profit);
  }
  $('#chance').keyup(updateValues);
  $('#bet').keyup(updateValues);
  $('#pay').keyup(updateValues);
  $('#profit').keyup(updateValues);
});

あなたの数学に何か問題があります。

ノート:

文字列を数値にするparseInt()必要はありません。parseFloat()数値である文字列の前の+記号は、それを数値に変換します。

詳細については、 http://jsfiddle.net/PHPglue/JQJMD/を参照してください。

于 2013-06-27T01:54:53.477 に答える