[SOLVED] Help with round()

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
User avatar
raymedia
Forum Commoner
Posts: 27
Joined: Thu Oct 09, 2003 6:36 am
Location: Melbourne, Australia

[SOLVED] Help with round()

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
raymedia
Forum Commoner
Posts: 27
Joined: Thu Oct 09, 2003 6:36 am
Location: Melbourne, Australia

Worked !!!! Thank you

Post by raymedia »

Worked !!!! Thank you a lot..:)
Post Reply