Rounding a number up

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Rounding a number up

Post by anthony88guy »

I need to be able to round a number up. I looked at the round() function, but is it possible to round it up.

i.e. 3.1 -> 4 , 3.9-> 4
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

ceil()
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

ok, i have a problem

Code: Select all

<?
$attackgold = $_POST&#1111;'attackgold'];
$defencegold = $_POST&#1111;'defencegold'];
$menuattack = $_POST&#1111;'attack'];
$menudefence = $_POST&#1111;'defence'];
$attackweapons = $attackgold/$attack;
$defenceweapons = $defencegold/$defence;

echo "<p><strong><font size="6">UNDER CONSTRUCTION</font></strong></p>"

if ($attackweapons > 0)&#123;
echo "ceil($attackweapons)";
&#125;else &#123;"";
&#125;

if ($defenceweapons > 0) &#123;
echo "ceil(defenceweapons)";
&#125;else &#123;"";
&#125;

?>
this is the error i get: Parse error: parse error, unexpected T_IF, expecting ',' or ';' in /home/nokiddin/public_html/bankingcalc2.php on line 27

any ideas?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your first echo is missing a semicolon.
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

Code: Select all

<? 
$attackgold = $_POST&#1111;'attackgold']; 
$defencegold = $_POST&#1111;'defencegold']; 
$menuattack = $_POST&#1111;'attack']; 
$menudefence = $_POST&#1111;'defence']; 
$attackweapons = $attackgold/$attack; 
$defenceweapons = $defencegold/$defence; 

echo "<p><strong><font size="6">UNDER CONSTRUCTION</font></strong></p>" 

if ($attackweapons > 0)&#123; 
echo ceil($attackweapons); 
&#125;else &#123;
echo""; 
&#125; 

if ($defenceweapons > 0) &#123; 
echo ceil(defenceweapons); 
&#125;else &#123;
echo ""; 
&#125; 

?>
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

Code: Select all

<?
$attackgold = $_POST&#1111;'attackgold'];
$defencegold = $_POST&#1111;'defencegold'];
$menuattack = $_POST&#1111;'attack'];
$menudefence = $_POST&#1111;'defence'];

if ($attackgold = 0)&#123;
echo "";
&#125;else&#123;
$attackweapons = $attackgold/$attack;
echo "ceil ($attackweapons)";
&#125;


if ($defencegold = 0)&#123;
echo "";
&#125;else&#123;
$defenceweapons = $defencegold/$defence;
echo "ceil ($defenceweapons)";
&#125;

echo "<p><strong><font size="6">UNDER CONSTRUCTION</font></strong></p>";
?>
this is the output: ceil (0)ceil (0)

i am looking for numbers, you can visit the site at http://nokidding.websiteallies.com/bankingcalc.html , check the bottom part, thats the one i am having trouble with.
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

when I type nothing into the gold field i get this error,

Warning: Division by zero in /home/nokiddin/public_html/bankingcalc2.php on line 26
ceil ()
Warning: Division by zero in /home/nokiddin/public_html/bankingcalc2.php on line 34
ceil ()
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your code is always setting $attackgold and $defencegold to zero. If $attack or $defence are zero, you will get a divide by zero error/warning..
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

new code

Code: Select all

<?
$attackgold = $_POST&#1111;'attackgold'];
$defencegold = $_POST&#1111;'defencegold'];
$menuattack = $_POST&#1111;'attack'];
$menudefence = $_POST&#1111;'defence'];

if ($attackgold != 0)&#123;
$attackweapons = $attackgold/$attack;
echo "ceil ($attackweapons)";
&#125;else&#123;
echo "";
&#125;


if ($defencegold != 0)&#123;
$defenceweapons = $defencegold/$defence;
echo "ceil ($defenceweapons)";
&#125;else&#123;
echo "";
&#125;

echo "<p><strong><font size="6">UNDER CONSTRUCTION</font></strong></p>";
?>
this code now works, although when it outputs the numbers it doesnt round up, it says ceil(1.77777) or something. Do i have the proper syntax?
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Don't echo the ceil function inside " ". Double quotes are for displaying string literals, you are trying to output the value of a function.

Wrong

Code: Select all

echo "ceil ($defenceweapons)";
Right

Code: Select all

echo ceil ($defenceweapons);
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

thxs guys i got it all to work :)
Post Reply