Simple and yet complicated Mathematics.

Hi all, I have this variable call $temp.

$temp is currently 6. But the variable result can be changing every time to a different number so it is not a fixed value.

For this $temp value, i will be using it to multiply it with 1.1666666, since currently the value is $temp is 6, the result will be 6.99999996.

Is there any way when the value is more then>*.49999 it will stay at *.5 instead of *?

Example: 6.51111111, 6.78948123, 6.9747124

Expected Output: 6.5

Example: 6.49999999, 6.12412431, 6.33452361

Expected Output: 6

Do note that, $temp value will be ever changing…so it will not be 6.5 or 6, it will be 7.5,7 or even 4.5, 4 depending on the output. thank you!

Are you looking for http://php.net/manual/en/function.round.php?

Round() won’t do it. What you want is to take the floor() of the number IF it the decimal portion is less than .5, but drop it to .5 if the decimal portion is greater than or equal to .5… am I right?

Need to write function for that.

What you’re going to want to do i your function is to first split the whole number out. Get your whole number into one variable, your decimal part into another.

Then, a simple if statement will work. If the decimal part if less that .5, return only the whle number. If greater than or equal to .5, return the whole number plus .5.

in other words:
[php]$toround = explode(".", $temp);
if ($toround[1] > 5 ) {
$temp = $toround[0];
} else {
$temp = ($toround[0] . “.5”);
}[/php]

… but I thought there was a function for this

Sponsor our Newsletter | Privacy Policy | Terms of Service