Currency rounding
This snippet:
<?php
$price=round(111,2);
$tax=round($price*0.175,2);
$total=round($price*1.175,2);
echo $price
";
echo "Tax at 17.5% $tax
";
echo "Total $total
";
?>
gives the result:
111
Tax at 17.5% 19.42
Total 130.43
Two questions.
Why the rounding difference?
and is there a standard function to force the 111 to display as 111.00
(given that price won't always be a whole number)?
Re: Currency rounding
On May 14, 12:01 pm, shortbackandsides... [at] spam.hairdresser.net wrote:
> This snippet:
> <?php
> $price=round(111,2);
> $tax=round($price*0.175,2);
> $total=round($price*1.175,2);
>
> echo $price
";
> echo "Tax at 17.5% $tax
";
> echo "Total $total
";
> ?>
> gives the result:
>
> 111
> Tax at 17.5% 19.42
> Total 130.43
>
> Two questions.
> Why the rounding difference?
> and is there a standard function to force the 111 to display as 111.00
> (given that price won't always be a whole number)?
When I run your code on my system I get 19.43 and 130.43. It could be
a platform or library issue -- I'm running PHP 5.1.6 on Fedora Core 6
Linux.
As for your other question, you can force the display of the zeros
using either number_format() or money_format():
<http://www.php.net/number_format>
<http://www.php.net/money_format>
Re: Currency rounding
On 14 May 2007 09:07:52 -0700, ZeldorBlat <zeldorblat [at] gmail.com>
wrote:
>On May 14, 12:01 pm, shortbackandsides... [at] spam.hairdresser.net wrote:
>> This snippet:
>> <?php
>> $price=round(111,2);
>> $tax=round($price*0.175,2);
>> $total=round($price*1.175,2);
>>
>> echo $price
";
>> echo "Tax at 17.5% $tax
";
>> echo "Total $total
";
>> ?>
>> gives the result:
>>
>> 111
>> Tax at 17.5% 19.42
>> Total 130.43
>>
>> Two questions.
>> Why the rounding difference?
>> and is there a standard function to force the 111 to display as 111.00
>> (given that price won't always be a whole number)?
>
>When I run your code on my system I get 19.43 and 130.43. It could be
>a platform or library issue -- I'm running PHP 5.1.6 on Fedora Core 6
>Linux.
>
>As for your other question, you can force the display of the zeros
>using either number_format() or money_format():
>
><http://www.php.net/number_format>
><http://www.php.net/money_format>
Thanks ZeldorBlat, it must be my PHP version that's the problem
(4.4.2) - money/number formats don't help either so it looks like time
for some fancy footwork with string manipulations.
Re: Currency rounding
On May 14, 8:15 pm, shortbackandsides... [at] spam.hairdresser.net wrote:
> On 14 May 2007 09:07:52 -0700, ZeldorBlat <zeldorb... [at] gmail.com>
> wrote:
>
>
>
> >On May 14, 12:01 pm, shortbackandsides... [at] spam.hairdresser.net wrote:
> >> This snippet:
> >> <?php
> >> $price=3Dround(111,2);
> >> $tax=3Dround($price*0.175,2);
> >> $total=3Dround($price*1.175,2);
>
> >> echo $price
";
> >> echo "Tax at 17.5% $tax
";
> >> echo "Total $total
";
> >> ?>
> >> gives the result:
>
> >> 111
> >> Tax at 17.5% 19.42
> >> Total 130.43
>
> >> Two questions.
> >> Why the rounding difference?
> >> and is there a standard function to force the 111 to display as 111.00
> >> (given that price won't always be a whole number)?
>
> >When I run your code on my system I get 19.43 and 130.43. It could be
> >a platform or library issue -- I'm running PHP 5.1.6 on Fedora Core 6
> >Linux.
>
> >As for your other question, you can force the display of the zeros
> >using either number_format() or money_format():
>
> ><http://www.php.net/number_format>
> ><http://www.php.net/money_format>
>
> Thanks ZeldorBlat, it must be my PHP version that's the problem
> (4.4.2) - money/number formats don't help either so it looks like time
> for some fancy footwork with string manipulations.
also in the UK tax is found by flooring not rounding. ie 17.5% of =A31
is 17.5p but the tax is only 17p
Re: Currency rounding
Message-ID: <1179193009.883361.18060 [at] q75g2000hsh.googlegroups.com> from
shimmyshack contained the following:
>> Thanks ZeldorBlat, it must be my PHP version that's the problem
>> (4.4.2) - money/number formats don't help either so it looks like time
>> for some fancy footwork with string manipulations.
Same here on 4.3.9
>
>also in the UK tax is found by flooring not rounding. ie 17.5% of £1
>is 17.5p but the tax is only 17p
I didn't know that. Have you got a reference?
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
Re: Currency rounding
On May 15, 7:15 am, Geoff Berrow <blthe... [at] ckdog.co.uk> wrote:
> Message-ID: <1179193009.883361.18060 [at] q75g2000hsh.googlegroups.com> from
> shimmyshack contained the following:
>
> >> Thanks ZeldorBlat, it must be my PHP version that's the problem
> >> (4.4.2) - money/number formats don't help either so it looks like time
> >> for some fancy footwork with string manipulations.
>
> Same here on 4.3.9
>
>
>
> >also in the UK tax is found by flooring not rounding. ie 17.5% of =A31
> >is 17.5p but the tax is only 17p
>
> I didn't know that. Have you got a reference?
> --
> Geoff Berrow 0110001001101100010000000110
> 001101101011011001000110111101100111001011
> 100110001101101111001011100111010101101011
OT.
In retail it is more complicated than just round down to nearest
penny!!
anyway this from the uk tax website:
17.5.1 Calculation based on lines of goods or services
If you wish to work out the VAT separately for a line of goods or
services, which are included with other goods or services in the same
invoice, you should calculate the separate amounts of VAT either by
rounding:
* down to the nearest 0.1p - for example, 86.76p would be rounded
down to 86.7p; or
* to the nearest 1p or 0.5p - for example, 86.76p would be rounded
up to 87p.
Whatever you decide, you must be consistent.
The final total amount of VAT payable may be rounded down to the
nearest whole penny.
http://customs.hmrc.gov.uk/channelsPortalWebApp/channelsPort alWebApp.portal=
?_nfpb=3Dtrue&_pageLabel=3DpageVAT_InfoGuides&propertyType=3 Ddocument&id=3D=
HMCE_CL_001543#P574_44405
Re: Currency rounding
Message-ID: <1179228680.850019.193580 [at] k79g2000hse.googlegroups.com> from
shimmyshack contained the following:
>OT.
>
>In retail it is more complicated than just round down to nearest
>penny!!
Yes it is!. Thanks for the info.
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
Re: Currency rounding
On Tue, 15 May 2007 13:21:36 +0100, Geoff Berrow
<blthecat [at] ckdog.co.uk> wrote:
>Message-ID: <1179228680.850019.193580 [at] k79g2000hse.googlegroups.com> from
>shimmyshack contained the following:
>
>>OT.
>>
>>In retail it is more complicated than just round down to nearest
>>penny!!
>
>
And we tripped over a shopping cart issue where rounding meant that
buying 10 items ended up costing a penny more than buying the ten
individually, inevitably someone spotted it and complained (yes, about
a one penny difference!)
The difference happens depending on whether you do the calculation
(item cost + vat) x 10
or (item cost x 10)+vat
Tax man is happy with either as long as you are consistent.
As for rounding down - oh bugger I'd just persuaded my script to round
both figures up consistently!