Page 1 of 1

All About the Money

Posted: Mon Feb 09, 2009 4:42 pm
by SheDesigns
I'm super confused trying to format this guys!! I'm making a shopping cart that tacks on 3%. I need the "cctotal" variable to have 2 numbers trailing the decimal (ex - 1.00, 5.25, 9.50). I don't care about a comma for a thousand dollar marker.

Sometimes, when I add the 3%, the number gets ugly, like 5.192059, I need it to be able to round up/down accordingly. Also, if it's just an even dollar amount (5.00, 10.00) or there is nothing in the tenth's place (5.10, 10.50) that is will show the 2nd trailing zero.

$onlinead = $onlinead * 1.03;
$cctotal = number_format($onlinead);

Right now, that won't give me trailing zeros I want!

Re: All About the Money

Posted: Mon Feb 09, 2009 4:45 pm
by VladSun

Re: All About the Money

Posted: Mon Feb 09, 2009 4:47 pm
by Drachlen
You're on the right track, just keep in mind that number_format can take additional parameters to handle this situation.

Take a look at this example:

Code: Select all

$cctotal = number_format(5.192059, 2, ".", ",");
echo $cctotal; 
This returns:

5.19

It will also return two trailing 0's (if there is nothing else.)

Good luck.