PDA

View Full Version : PHP round() question



sigepmest37
September 7th, 2006, 04:41 PM
I've been trying to come up with a solution to a little PHP round() problem I'm having.

I'm trying to add tax to a subtotal...pretty simple:



$tax= round(($subTotal*.0825),2);


Most of the time this works fine, however, on the rare occasion that the tax comes back as a whole dollar or whole 10 cents, it drops the extra 0's at the end.

Is there a way to check if that has happened and add the necessary 0's (1 or 2) to the end of the string if needed?

Thanks for the help guys!!!

bwh2
September 7th, 2006, 04:59 PM
use regex to grab the length of the string after the period. concatenate on the appropriate # of zeros using str_repeat.

λ
September 7th, 2006, 05:20 PM
or use sprintf:



print sprintf("%.2f", $subTotal*.0825);

bwh2
September 7th, 2006, 05:23 PM
word. definitely the better solution.

sigepmest37
September 7th, 2006, 05:25 PM
or use sprintf:



print sprintf("%.2f", $subTotal*.0825);


Thanks, that works perfectly!

λ
September 7th, 2006, 05:27 PM
Glad I could help =)