Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

PHP Round Up Question

Hey guys,

I'm working on an OpenCart store but my client's being picky and wants numbers rounding up to 50p (we're in the UK).. so for example, £3.03 would be £3.50.. and £6.67 would be £7.00

Now I've found the bit of code that I need to replace..

    $string .= number_format(round($value, (int)$decimal_place), (int)$decimal_place, $decimal_point, $thousand_point);
    ``` 

and i've changed it to

 ```php
    $string .= number_format(ceil($value), (int)$decimal_place, $decimal_point, $thousand_point);
    ``` 

Which rounds it from £42.01 to £43.00 instead of £42.50... Does anyone know how to adjust it to make it go to .50 instead of the next pound?

Many thanks!

Ben

1 Answer

It is a bit of a work-around solution, but if you multiply by two, round to the nearest £, then divide the result by two, then you'll have your number rounded to 50p:

    $string .= (2 * number_format(ceil($value), (int)$decimal_place, $decimal_point, $thousand_point))/2;

Something like that. Give it a go.

Thanks John, turns out my client wanted it rounded up to the nearest 5p not 50.. so I rejigged and came up with this function that did the trick...

function roundFive($v) {

        return ceil($v*20) / 20;
}