/    Sign up×
Community /Pin to ProfileBookmark

Round UP to the closet 2 decimal places ??

Hi,

I am using this to work out the VAT on an item (17.5%)

[code=php]
$VAT_Price = (((17.5/100) * $UnitPrice) + $UnitPrice)
[/code]

The problem is if the unit price is 3.45 the $VAT_Price is coming out at 4.05375 but it can only be two decimal places ??

Thanks
Ada,

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 19.2005 — [code=php]
$VAT_Price = sprintf("%.2f", (17.5/100 * $UnitPrice) + $UnitPrice + 0.005);
[/code]
Copy linkTweet thisAlerts:
@k0r54authorApr 19.2005 — ahh thank you ?

i new it was something simple
Copy linkTweet thisAlerts:
@k0r54authorApr 19.2005 — sorry but what is ther +0.005 about?

is that to round it up?
Copy linkTweet thisAlerts:
@NogDogApr 19.2005 — sorry but what is ther +0.005 about?

is that to round it up?[/QUOTE]

Yep. ?

sprintf("%.2f", $value) will round $value to the nearest hundredth, so adding .005 makes sure that it always rounds up. (Possibly adding .00499999 would be better?)
Copy linkTweet thisAlerts:
@ShrineDesignsApr 19.2005 — or you could do this[code=php]<?php
$vat_price = ((17.5 / 100) * $unitprice) + $unitprice;
echo round($vat_price, 3);
?>[/code]
Copy linkTweet thisAlerts:
@JuuitchanApr 20.2005 — No. Do the arithmetic in cents (because these wretched machines insist on binary) and then round up.
×

Success!

Help @k0r54 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...