Viscocent
Viscocent

Reputation: 2064

jQuery append incorrectly displaying float number

its been an hour since i started working on this and i cand find the problem

this is my code inside the loop

$.each(data,function(key,game){
    console.log(game.teams_odds[1].game_odds_value_modified);
    var home_odds = parseFloat(game.teams_odds[0].game_odds_value_modified).toFixed(2);
    var away_odds = parseFloat(game.teams_odds[1].game_odds_value_modified).toFixed(2);
    console.log(away_odds);
    $("#oddsTable").append('<tr>' +
                                '<td>' +
                                    '<span class="red">Game ID : '+game.game_id+' </span>' +
                                        '<br>'+game.game_start_time+'<br>' +
                                '</td>' +
                                '<td>' +
                                    '<span class="teamName">'+game.teams_odds[0].team_name_eng+'</span>' +
                                    '<span class="teamName">'+game.teams_odds[1].team_name_eng+'</span>' +
                                '</td>' +
                                '<td>' +
                                    '<span style="display: block">' +
                                        '<a href="{if $isDotNet == 0}/{$kioskUrl}/single-pop/{$dataHolders.team_home_game_details}/bettype/{$betType}/baseball/{else}/pop_bet.php?gdid={$dataHolders.team_home_game_details}{/if}" class="teamOdds{if $dataHolder.blink_it == 1 && $dataHolder.blink_it_game_details_id == $dataHolder.game_details_id_home} blinkMe{/if}" style="font-size: 15px; font-weight: bold;">' +
                                            +home_odds+
                                        '</a>' +
                                    '</span>' +
                                    '<span style="display: block">' +
                                        '<a href="{if $isDotNet == 0}/{$kioskUrl}/single-pop/{$dataHolders.team_away_game_details}/bettype/{$betType}/baseball/{else}/pop_bet.php?gdid={$dataHolders.team_away_game_details}{/if}" class="teamOdds{if $dataHolder.blink_it == 1 && $dataHolder.blink_it_game_details_id == $dataHolder.game_details_id_away} blinkMe{/if}" style="font-size: 15px; font-weight: bold;">' +
                                            +away_odds+
                                        '</a>' +
                                    '</span>' +
                                    '<br>'+game.game_start_time+'<br>' +
                                '</td>' +
                           '</tr>'
                           );
});

when i try to use console.log() and alert() to show the values just fine, but when i see my rendered html from using append it the number lacks the number zero zero

say 1.60 becomes 1.6

console.log(away_odds) //1.60
alert(away_odds) //1.60

what i want is for it to display 1.60 but it keeps displaying 1.6

Upvotes: 0

Views: 71

Answers (2)

Teemu
Teemu

Reputation: 23406

You've concatenated the previous line already:

'<a href="{if $isDotNet == 0} ... style="font-size: 15px; font-weight: bold;">' +
    +away_odds+ //                                                    extra + --^
'</a>' +

Leading + on the second line is actually Unary Plus operator, which converts away_odds to a number again. Also home_odds will be converted to a number for the same reason.

Upvotes: 3

mohamedrias
mohamedrias

Reputation: 18566

toFixed(2) should only be used in the part of the code that outputs it.

In this case,

var home_odds = parseFloat(game.teams_odds[0].game_odds_value_modified);
var away_odds = parseFloat(game.teams_odds[1].game_odds_value_modified);

And where you are printing the variables use

home_odds.toFixed(2)

I have tested it and it works fine

Upvotes: 0

Related Questions