All About the Money

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
SheDesigns
Forum Commoner
Posts: 42
Joined: Tue Nov 18, 2008 9:51 am
Location: Buffalo, NY

All About the Money

Post 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!
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: All About the Money

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Re: All About the Money

Post 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.
Post Reply