Page 1 of 1

[SOLVED] Help with round()

Posted: Wed Apr 07, 2004 3:15 am
by raymedia
Hi guys..need some hint or help. I founf there were few examples of round function in php..but its not helping me much..

my question suppose to be pretty basic.

ie. I got the value of $price from mysql db.

Code: Select all

$price = number_format($price,2);
and yeah..i got the right..but i need that to be rounded to nearest $0.50..ie..if $12.43 ..i need somehow it return $12.50...i know it might use round function..but not sure..thank a lot guys

Posted: Wed Apr 07, 2004 3:34 am
by twigletmac
Adapted this from the user comments found below the information for [php_man]round[/php_man]():

Code: Select all

<?php

$price1 = 12.48;
$price2 = 12.02;
$price3 = 12.56;
$price4 = 12.76;

$num1 = round($price1/0.5) * 0.5;
$num2 = round($price2/0.5) * 0.5;
$num3 = round($price3/0.5) * 0.5;
$num4 = round($price4/0.5) * 0.5;

echo number_format($num1, 2);
echo '<br />';
echo number_format($num2, 2);
echo '<br />';
echo number_format($num3, 2);
echo '<br />';
echo number_format($num4, 2);

?>
Mac

Worked !!!! Thank you

Posted: Fri Apr 09, 2004 12:37 am
by raymedia
Worked !!!! Thank you a lot..:)