supanoob Posted November 24, 2007 Share Posted November 24, 2007 so basically i am coding a text based game, i was using number_format(); to format my numbers but when i went to subtract or add to them it wouldnt work. for example i needed to heal which costs "gold". i would have: $current_health=($max_health-$health); $cost = ($current_health/2); $cost=round($cost); then they would click a link and it would tell them they dont have enough to heal, even though they would have enough. Is there a reason for this? or another way to format numbers? Quote Link to comment https://forums.phpfreaks.com/topic/78675-solved-number_format/ Share on other sites More sharing options...
Wuhtzu Posted November 24, 2007 Share Posted November 24, 2007 I guess we need to see the code where you use number_format() Quote Link to comment https://forums.phpfreaks.com/topic/78675-solved-number_format/#findComment-398096 Share on other sites More sharing options...
supanoob Posted November 24, 2007 Author Share Posted November 24, 2007 ok ill modify the top post now. It wouldnt let me so here it is <?php if ($_GET['step'] == 'main') { if ($health == $max_health) { echo "You do not need to heal."; die(include_once('logged_in_bottom.php')); } $current_health=($max_health-$health); $cost = ($current_health/2); $cost=round($cost); $gold=number_format($gold); $cost=number_format($cost); echo "It would seem you are hurt. It will cost you $cost to heal. Continue? [<a href=\"hospital.php?step=1\">Yes</a> | <a href=\"home.php?step=main\">No</a>]"; } if ($_GET['step'] == '1') { $current_health=($max_health-$health); $cost = ($current_health/2); $cost=round($cost); $gold=number_format($gold); $cost=number_format($cost); if ($gold < $cost) { echo "You cannot afford to heal yourself."; die(include_once('logged_in_bottom.php')); } $sql2="UPDATE accounts SET health='$max_health', gold=gold-$cost WHERE account_id='$account_id'"; if(mysql_query($sql2)) echo "You have been healed back to full life."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/78675-solved-number_format/#findComment-398097 Share on other sites More sharing options...
Wuhtzu Posted November 24, 2007 Share Posted November 24, 2007 Have a look at the following examples (and run them): <?php $a = 7331; $b = 1337; $c = number_format($a); $d = number_format($b); //Outputs 5994 (correct) echo $a - $b . '<br>'; //Outputs 6 (incorrect) echo $c - $d . '<br>'; ?> <?php $a = 1337; $b = 6; $c = number_format($a); $d = number_format($b); //Outputs 'a > b' if($a > $b) { echo 'a > b'; } else { echo 'a < b'; } //Outputs 'c < d' if($c > $d) { echo 'c > d'; } else { echo 'c < d'; } ?> <?php $a = 1337; $b = 12345; $c = number_format($a); $d = number_format($b); echo $a //1337 echo $b //12345 echo $c //1,337 echo $d //12,345 You get some real "funny" results if you use number_format() on your numbers before doing calculations. number_format() is for formatting numbers for presentation. Why do you use it in your code? Quote Link to comment https://forums.phpfreaks.com/topic/78675-solved-number_format/#findComment-398145 Share on other sites More sharing options...
supanoob Posted November 24, 2007 Author Share Posted November 24, 2007 i didnt to start with, at first i just had it to present it in the stats section, but thenit stopped the hosp working and then i tried that and that didnt work, so i had to remove it. Quote Link to comment https://forums.phpfreaks.com/topic/78675-solved-number_format/#findComment-398156 Share on other sites More sharing options...
Wuhtzu Posted November 24, 2007 Share Posted November 24, 2007 Did you study the examples I gave above? This for example could cause trouble: <?php $gold=number_format($gold); $cost=number_format($cost); if ($gold < $cost) { } ?> Keep number_format() out of any non-presentational code. Use something like this: <?php $gold = 1337; $gold = $gold + 100; echo "You now have " . number_format($gold) . " gold!"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/78675-solved-number_format/#findComment-398159 Share on other sites More sharing options...
supanoob Posted November 24, 2007 Author Share Posted November 24, 2007 Did you study the examples I gave above? This for example could cause trouble: <?php $gold=number_format($gold); $cost=number_format($cost); if ($gold < $cost) { } ?> Keep number_format() out of any non-presentational code. Use something like this: <?php $gold = 1337; $gold = $gold + 100; echo "You now have " . number_format($gold) . " gold!"; ?> thanks will do that Quote Link to comment https://forums.phpfreaks.com/topic/78675-solved-number_format/#findComment-398173 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.