Jump to content

[SOLVED] number_format();


supanoob

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/78675-solved-number_format/
Share on other sites

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.";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/78675-solved-number_format/#findComment-398097
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/78675-solved-number_format/#findComment-398145
Share on other sites

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!";
?>

Link to comment
https://forums.phpfreaks.com/topic/78675-solved-number_format/#findComment-398159
Share on other sites

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 :D will do that

Link to comment
https://forums.phpfreaks.com/topic/78675-solved-number_format/#findComment-398173
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.