number_format without rounding

Hi All

This is doing my head in!!!

I am using:

if ($country == ‘AU’) { $hasgst = ‘’; $gstamt = $unitprice * .1; $exgst = $unitprice - $gstamt; } else { $hasgst = ‘n’; $exgst = $unitprice; $gstamt = “”; }

$unitprice = (number_format($unitprice, 2, ‘.’, ‘,’));
$exgst = (number_format($exgst, 2, ‘.’, ‘,’));
$gstamt = (number_format($gstamt, 2, ‘.’, ‘,’));

Basically this code works out the GST (tax) of the total amount.

The issue I have is that the current price is $49.95 (this can change to whatever though) and when the code above does its job the number_format option is rounding up putting everything out by .01.

I have read a pile of post I Googled but they are all different.

Can anyone please tell me how to stop the rounding?

Thanks

mrmbarnes

Have a look in the PHP manual at the money_format() function, it should suit your needs better then number_format

So 49.95;
10% = 4.995
total = 54.945

Your post suggests you want to show 54.94 rather than 54.95. (Why? I’m fairly sure the tax system would round this up. But anyway…)

money_format still rounds, but is a good currency display.

floor($total * 100) / 100 will give you the floored value 54.94.

Thanks guys… the rounding was rounding both up… the 1 cent on tax or one cent of non tax doesn’t really matter as that could be open for discussion but the issue I was having is that the total was adding up to $49.96 and not $49.95.

In any case the money_format() function sorted it all out.

Thanks once again