2016-10-30 24 views
0

パラメータを文字列 として取得する関数に問題がありましたが、浮動小数点数にする必要があります(整数ではありません)Javascript/jQuery - 浮動小数点数と浮動小数点数()への問題

私の例では
function(costsForThisPlayer){ 
    moneyRemaining += costsForThisPlayer;    
    moneyRemaining2Decimals = parseFloat(moneyRemaining).toFixed(2); 
} 

は今、私のcostsForThisplayerは "1.0"

だったが、今、私のmoneyRemainingは次のようになります。

991.00

こと私のmoneyRemainingは99

だったので、私のコードは値を追加する代わりにnrの最後に値を追加するだけです。

誰も考えがありますか?

は私が

  • を浮遊した後、小数点以下2桁

でtoFixed()とmoneyRemainingを作るために変換すると思ったが、それは仕事doenst

EDIT:

これは私の全体ですjsコード

function addGoalkeeperByClickEvent(playerName, id, costsForThisPlayer) { 
$(document).off('click', '.goalKepperRow, .goalkeeperGreyRow').on('click', '.goalKepperRow, .goalkeeperGreyRow', function() { 

    $('#goalkeeperLine div p').first().parent().html("<div id='"+id+"' costsForThisPlayer='"+costsForThisPlayer+"'><img src='https://d34h6ikdffho99.cloudfront.net/uploads/real_team/shirt/1188/shirt-333.svg'><span class='ui-icon ui-icon-circle-close remove' style='top: -69.0833px; left: 34.3167px;'></span><div>".concat(playerName)); 


    console.log("GK wurde mit Row Click entfernt:"); 

    $(this).attr('class', 'goalKepperRow'); 
    GoalKeeperQuantity--; 
    totalPlayersOnField--; 


    moneyRemaining += costsForThisPlayer; 
    moneyRemaining2Decimals = parseFloat(moneyRemaining).toFixed(2);     
    $("#moneyRemaining").html(moneyRemaining2Decimals+" $ (Mio)"); 


    $("#moneyRemaining").html(moneyRemaining2Decimals+" $ (Mio)");  

    console.log("bis hier hin gehts"); 

    $("#field #".concat($(this).attr('id'))).empty(); 
    $("#field #".concat($(this).attr('id'))).parent().html("<p></p>"); 

    $("#players").html(totalPlayersOnField+"/12");  
}); 

}

+0

は効果がありませんcostsForThisPlayerについても、Stringとして扱われています。 –

+0

@ClainDsilva申し訳ありませんが、私は第1稿を編集しました。parseFloat()で試してみましたが、これは誤解のために申し訳ありませんでした... 他に何が問題になるのでしょうか? – nbg15

答えて

1

costsForThisPlayerにparseFloatを適用して、Float型の両方を追加する必要があります。

function(costsForThisPlayer){ 
     moneyRemaining = parseFloat(moneyRemaining) + parseFloat(costsForThisPlayer); 
     //use moneyRemaining.toFixed(2) on remaining code       
    } 
+0

@AndreasはmoneyRemainingがfloat型であると仮定しています。それ以外にもparseFloatを適用します。浮動小数点型の両方を作成しました。更新された答えを参照してください。 –

+0

私の最初の投稿にコード全体が追加されました... – nbg15

+0

@Andreas正しい、私はあなたのポイントを参照してくださいコードに適用された修正 –

0

OK、これは私のコードは今、整数は、小数点を持っていない、どちらかあなたはparseFloatはを適用再び、parseFloatは使用する必要がありますので、のparseInt()にtoFixedを適用)

moneyRemaining += parseFloat(costsForThisPlayer); 
moneyRemaining2Decimals = moneyRemaining.toFixed(2);     
$("#moneyRemaining").html(moneyRemaining2Decimals+" $ (Mio)"); 
関連する問題