Be Excellent To Each Other

And, you know, party on. Dude.

All times are UTC [ DST ]




Reply to topic  [ 24 posts ] 
Author Message
 Post subject: VAT calculation.
PostPosted: Mon Dec 07, 2009 22:52 
User avatar

Joined: 30th Mar, 2008
Posts: 14308
Location: Shropshire, UK
Apparently.

Right, let's assume we're working with 15% VAT here, obv.

I have a PHP function that takes a price excluding VAT, adds 15% VAT to it, and returns that value. Here it is, albeit in simplified form:
Code:
function getPriceIncVAT($price, $taxrate = 15) {
   $tax = round(($taxrate / 100) * $price, 2);
   return round($price + $tax, 2);
}


Nice and simple, and it works well. Except in very specific circumstances.

If I put in £8.70, for example (which should be £10.00 inc VAT) it returns £10.01, because it works the calculation out as £10.005 and rounds it up.

I've tried changing the round functions to floor() or ceil() and that fixes £8.70, but then breaks other prices.

So, what is the correct, HMRC-approved way of calculating VAT?


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Mon Dec 07, 2009 22:57 
User avatar
Sitting balls-back folder

Joined: 30th Mar, 2008
Posts: 10130
The normal way I see in maths for mass spec is to either add or subtract 0.5 from the /100 bit, depending on which works properly. Or maybe from price + tax. Something like that, anyway.

You probably don't really want to round twice, do you?


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Mon Dec 07, 2009 23:06 
User avatar
Isn't that lovely?

Joined: 30th Mar, 2008
Posts: 11124
Location: Devon
Yeah, is it do with storing the tax amount as a rounded number?

just have

$tax = $taxrate * $price / 100 ;

?

Malc

_________________
Where's the Kaboom? I was expecting an Earth shattering Kaboom!


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Mon Dec 07, 2009 23:06 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69664
Location: Your Mum
I think we use banker's rounding, which is something like rounding .5p to end on an even number.

Aha - like so: http://en.wikipedia.org/wiki/Rounding#R ... lf_to_even

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Mon Dec 07, 2009 23:22 
User avatar
Excellent Member

Joined: 25th Nov, 2008
Posts: 1041
I think what you need here is a Decimal (or Currency) type that will typically implement Bankers Rounding. I don't know if PHP supports such constructs, but if not you could roll your own.

_________________
Image


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Mon Dec 07, 2009 23:24 
User avatar
Sitting balls-back folder

Joined: 30th Mar, 2008
Posts: 10130
Yeah, the thing I said - undoubtedly inaccurately - balances banker's rounding against always-up that computers do. I think.


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Mon Dec 07, 2009 23:48 
User avatar

Joined: 30th Mar, 2008
Posts: 14308
Location: Shropshire, UK
Legends, you guys are. Banker's rounding is indeed what I needed.


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Tue Dec 08, 2009 9:56 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69664
Location: Your Mum
EoaE clearly has me on ignore.


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Tue Dec 08, 2009 9:57 
User avatar
UltraMod

Joined: 27th Mar, 2008
Posts: 55717
Location: California
Quote:
You cannot add administrators and moderators to your foes list.


:(

_________________
I am currently under construction.
Thank you for your patience.


Image


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Tue Dec 08, 2009 10:32 
User avatar
baron of techno

Joined: 30th Mar, 2008
Posts: 24136
Location: fife
Has anyone suggested Bankers' Rounding yet?


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Tue Dec 08, 2009 10:47 
SupaMod
User avatar
Commander-in-Cheese

Joined: 30th Mar, 2008
Posts: 49237
I think Alastair Darling is going to be changing the definition of Bankers' Rounding this week.

_________________
GoddessJasmine wrote:
Drunk, pulled Craster's pork, waiting for brdyime story,reading nuts. Xz


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Tue Dec 08, 2009 10:48 
User avatar
Skillmeister

Joined: 27th Mar, 2008
Posts: 27023
Location: Felelagedge Wedgebarge, The River Tib
It's going to be called Bankers' Softball from now on.

_________________
Washing Machine: Fine. Kettle: Needs De-scaling. Shower: Brand new. Boiler: Fine.
Archimedes Hotdog Rhubarb Niner Zero Niner.


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Tue Dec 08, 2009 10:49 
User avatar
Hibernating Druid

Joined: 27th Mar, 2008
Posts: 49276
Location: Standing on your mother's Porsche
Dirty

_________________
SD&DG Illustrated! Behance Bleep Bloop

'Not without talent but dragged down by bass turgidity'


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Tue Dec 08, 2009 11:11 
User avatar
Excellent Member

Joined: 30th Mar, 2008
Posts: 5924
Location: Stockport - The Jewel in the Ring
Wankers Rounding?

_________________
Mint To Be Stationery - Looking for a Secret Santa gift? Try our online shops at Mint To Be.

Book me in the Face | Tweet me. Tweet me like a British nanny.


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Tue Dec 08, 2009 11:12 
User avatar
Skillmeister

Joined: 27th Mar, 2008
Posts: 27023
Location: Felelagedge Wedgebarge, The River Tib
Deep & Plissken Even wrote:
Wankers Rounding?


Dangerous chance of whiplash.

_________________
Washing Machine: Fine. Kettle: Needs De-scaling. Shower: Brand new. Boiler: Fine.
Archimedes Hotdog Rhubarb Niner Zero Niner.


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Tue Dec 08, 2009 12:08 
User avatar
Excellent Member

Joined: 25th Nov, 2008
Posts: 1041
Grim... wrote:





Eh? ;)

_________________
Image


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Tue Dec 08, 2009 17:56 
User avatar

Joined: 30th Mar, 2008
Posts: 14308
Location: Shropshire, UK
Nah, something still isn't right, although my experiences now are mirroring those found on all of the VAT calculators on the 'net (even Digita and other tax accounting firms)

Assume a net price of £34.00.

Take the VAT off, and you're left with £29.57 (£4.43 VAT)

Now, if you put 15% VAT back on 29.57 you get £4.44 VAT for a total of £34.01.

It seems impossible to avoid some sort of rounding error somewhere when dealing with tax. That ain't good.


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Tue Dec 08, 2009 18:08 
User avatar
Excellent Member

Joined: 25th Nov, 2008
Posts: 1041
Are you rounding the second calculation (ie 29.57 * 1.15)?

_________________
Image


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Tue Dec 08, 2009 18:09 
User avatar
baron of techno

Joined: 30th Mar, 2008
Posts: 24136
Location: fife
It's nothing to do with tax, it's just because you're working at a resolution which is the same as the minimum unit of currency.

Store the before-tax amount to 4 decimal places, or just tweak it so that the with-tax value is what you want (i.e. 33.99).

You shouldn't be converting back and forth anyway, just do one conversion.


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Tue Dec 08, 2009 18:12 
User avatar
Heavy Metal Tough Guy

Joined: 31st Mar, 2008
Posts: 6580
Skim off those extra pennies into your own private account. You'll make millions and they'll never even notice.


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Tue Dec 08, 2009 18:15 
User avatar
baron of techno

Joined: 30th Mar, 2008
Posts: 24136
Location: fife
http://customs.hmrc.gov.uk/channelsPort ... 387_111313


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Tue Dec 08, 2009 18:20 
User avatar
Hibernating Druid

Joined: 27th Mar, 2008
Posts: 49276
Location: Standing on your mother's Porsche
2 Squirtledoves wrote:
Skim off those extra pennies into your own private account. You'll make millions and they'll never even notice.

Careful.
Image

_________________
SD&DG Illustrated! Behance Bleep Bloop

'Not without talent but dragged down by bass turgidity'


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Tue Dec 08, 2009 18:33 
User avatar

Joined: 30th Mar, 2008
Posts: 14308
Location: Shropshire, UK
kalmar wrote:
You shouldn't be converting back and forth anyway, just do one conversion.

My customers want the flexibility to input their prices either in net or gross, but I want to store the prices gross so that the items can be resold in countries with different tax rates without the system throwing a fit.

Think I've got a handle on it now anyway.


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Tue Dec 08, 2009 18:58 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69664
Location: Your Mum
2 Squirtledoves wrote:
Skim off those extra pennies into your own private account. You'll make millions and they'll never even notice.

Image

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: VAT calculation.
PostPosted: Wed Dec 09, 2009 19:43 
User avatar
Participant in dramatic games

Joined: 30th Mar, 2008
Posts: 4151
Location: United Provinces
kalmar wrote:
Has anyone suggested Bankers' Rounding yet?

i just discovered on grim's link that some people call it dutch rounding, so clearly i'm entitled to suggest it once more

_________________
XBL: Romanista WiiU: Romanista77 Gamecenter: Romanista345 3DS 0318 8943 6467
Steam: Romanista345 PSN: Romanista345 Switch: 5098 6135 1325 RetroAchievements: Romanista
[img=500x70]https://card.exophase.com/2/0/252928.png?1717524262[/IMG]


Top
 Profile  
 
Display posts from previous:  Sort by  
Reply to topic  [ 24 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: NervousPete and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search within this thread:
You are using the 'Ted' forum. Bill doesn't really exist any more. Bogus!
Want to help out with the hosting / advertising costs? That's very nice of you.
Are you on a mobile phone? Try http://beex.co.uk/m/
RIP, Owen. RIP, MrC. RIP, Dimmers.

Powered by a very Grim... version of phpBB © 2000, 2002, 2005, 2007 phpBB Group.