Forum Moderators: coopster

Message Too Old, No Replies

Rounding up

how?

         

petra

6:05 pm on Jan 8, 2007 (gmt 0)

10+ Year Member



I've been trying to round up a figure from a shopping feed so that instead of displaying 6.989 it displays 6.99.

Basically I want to round up all the figures in the $Price field from the mysql database.

I've played around with the php round function but to no avail!

Any help would be appreciated.

Many thanks

brevetoxin

6:20 pm on Jan 8, 2007 (gmt 0)

10+ Year Member



Try this:

$number = 6.989;
$newNumber = number_format($number,2);

That will cut $number to 2 decimal places, rounding it accordingly.

whoisgregg

6:53 pm on Jan 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to ensure it always rounds up, try something along these lines:

$number = 6.981;
$newNumber = [url=http://php.net/number_format]number_format[/url]( ( ( [url=http://php.net/ceil]ceil[/url]($number * 100) ) / 100 ) , 2);
echo $newNumber; // 6.99

petra

8:01 pm on Jan 8, 2007 (gmt 0)

10+ Year Member



Thanks for that, however its not working, where does the code go? and what about implementing it to a $variable and not a specific number?

Thanks again!

petra

8:10 pm on Jan 8, 2007 (gmt 0)

10+ Year Member



Figured it out , replaced actual number with $variable.

Thanks to both of you!

Cheers!

supermanjnk

2:56 am on Jan 9, 2007 (gmt 0)

10+ Year Member



I always use this when I always want to round up (normally with pagination scripts)

$value = 3.392
$num = round($value +.5);

whoisgregg

4:52 am on Jan 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's a much better way than mine, supermanjnk. Thanks for posting it. :)