Cannot output integer in PHP

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
beechgrove
Forum Newbie
Posts: 3
Joined: Sun Sep 09, 2012 3:50 am

Cannot output integer in PHP

Post by beechgrove »

Hi

I am still pretty new to PHP and have written a calculator script that takes Number1, divides it by 100, multiplies it by Number 2 then finally divides it by 20 (trust me there is a purpose to this!!)

The problem is that it (for instance) when Number1 is 254 and Number2 is 480 it returns 60.9600 when what I actually want it to return is 61.

I have tried every round(), floor() etc statement I can find but it still returns 60.9600!!!

The calculation is: $answare = $number1/100*$number2/20;

Please, please can someone tell me how to return just the integer part?

Thanks
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Cannot output integer in PHP

Post by social_experiment »

round() should work in this instance; can you paste the code in question
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
beechgrove
Forum Newbie
Posts: 3
Joined: Sun Sep 09, 2012 3:50 am

Re: Cannot output integer in PHP

Post by beechgrove »

Hi - thanks for the reply. I have posted below all the code for 4 files being used. I am basically trying to adapt a free calculator program to carry out the calculations for my sister to use for her diet plan!

calc.php (this is the form used for entering the data)
<head>
<title>Calculator</title>
<link rel="stylesheet" href="calcstyle.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

</head>
<body>
<center>
<div class="topbox"><b>Calculator</b></div>
<form method="get" action="calculation.php">
<b>kCals</b> per 100g:<br />
<input class="textbox" type="text" name="number1" /><br />
<b>Pack size</b> (g):<br />
<input class="textbox" type="text" name="number2" /><br />
<b>Operation:</b><br />
<input class="button" type="submit" name="op" value="Calculate" />
<input class="cbutton" style="color:#ff0000;" type="submit" name="op" value="C" />
</form>
<div class="bottombox"> Result per pack: <b><?php include('ans.html'); ?></b></div>
</center>
</body>
</html>

calcstyles.css (style sheet)

/*
* CSS Document for calc.php: calcstyle.css
*/

html {
width:100%;
background:#ffffff;
}

body {
width:auto;
background:#ffffff;
font-family:verdana,arial,sans-serif;
font-size:10px;
color:#000000;
}

.topbox {
width:200px;
padding:5px;
background:#ccccff;
border:1px solid #555555;
margin-bottom:5px;
}

.bottombox {
width:200px;
padding:5px;
background:#ccccff;
border:1px solid #555555;
margin-top:5px;
}

form {
width:200px;
padding:5px;
margin:0px;
background:#f2f2f2;
border:1px solid #555555;
}

.textbox {
border:1px solid #555555;
background:#ffffff;
}

.button {
width:130px;
height:25px;
background:#cccccc;
}

.cbutton {
width:30px;
height:25px;
background:#ffcc00;
}


calculation.php (the calculator program)
<?php

$number1 = $_GET['number1'];
$number2 = $_GET['number2'];
/* $op = $_GET['op'];*/
$op = '/';
$perpack = " per pack";

// start of "/" operation

if ( $op == "/" )
{
$filename = 'ans.html';
$answare = round($number1/100*$number2/20);



// Checking of writability of the "ans.html" file,
// don't forget to CHMOD it to value
// of 777 e.g. rwx.rwx.rwx.
if (is_writable($filename)) {

// The data in the "ans.html" will be
// overwrited with every new calulation.
if (!$handle = fopen($filename, 'r+')) {
echo "Cannot open file ($filename)";
exit;
}

// Write $answare data to "ans.html" file.
if (fwrite($handle, $answare) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
// Redirecting the data to the main
// "calc.php" page to be displayed.
echo ('<meta http-equiv="refresh" content="0;url=calc.php" />');

fclose($handle);

} else {
echo "The file $filename is not writable";
}
}

// start of "C" operation
// This will empty the numbers fields
// and place the "_" sign at the "ans.html" file
// by resetting the whole "calc.php" page.

if ( $op == "C" )
{
$filename = 'ans.html';
$answare = "_";

// Checking of writability of the "ans.html" file,
// don't forget to CHMOD it to value
// of 777 e.g. rwx.rwx.rwx.
if (is_writable($filename)) {

// The data in the "ans.html" will be
// overwrited with every new calulation.
if (!$handle = fopen($filename, 'wb')) {
echo "Cannot open file ($filename)";
exit;
}

// Write $answare data to "ans.html" file.
if (fwrite($handle, $answare) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
// Redirecting the data to the main
// "calc.php" page to be displayed.
echo ('<meta http-equiv="refresh" content="0;url=calc.php" />');

fclose($handle);

} else {
echo "The file $filename is not writable";
}
}

?>


ans.html (the output file - this must be set to 777)

_
beechgrove
Forum Newbie
Posts: 3
Joined: Sun Sep 09, 2012 3:50 am

Re: Cannot output integer in PHP

Post by beechgrove »

I have just played around with this a bit more and reinstate the GET statement and it all works now, thanks Odd!
Post Reply