Dollars and Cents

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
mariolopes
Forum Contributor
Posts: 102
Joined: Sun May 22, 2005 7:08 am

Dollars and Cents

Post by mariolopes »

Hi
I need to save on price in my table. I get the dollars and cents separately. How can i save the full amount?
Thank you
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Dollars and Cents

Post by pickle »

Concatenate

Code: Select all

$dollars = 12;
$cents = 95;
 
$full = $dollars.'.'.$cents;
Or add

Code: Select all

$dollars = 12;
$cents = 95;
 
$full = $dollars + ($cents/100);
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mariolopes
Forum Contributor
Posts: 102
Joined: Sun May 22, 2005 7:08 am

Re: Dollars and Cents

Post by mariolopes »

Thank you
but i get one string: $full = $dollars.'.'.$cents;
How can i convert this string to money, because i want to save his value in mysql table?
Thank you
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Dollars and Cents

Post by Eran »

If we could convert strings to money, we'd all be filthy rich by now...

Seriously though, you need to explain what you meant by that. 'money' is not a data type
mariolopes
Forum Contributor
Posts: 102
Joined: Sun May 22, 2005 7:08 am

Re: Dollars and Cents

Post by mariolopes »

You're right
I need to convert string -> decimal. I think i found the answer at http://php.net/manual/en/function.intval.php
Thank you
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Dollars and Cents

Post by Eran »

I don't think intval() is what you're looking for then, perhaps number_format() is what you need - http://php.net/manual/en/function.number-format.php
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Dollars and Cents

Post by AbraCadaver »

mariolopes wrote:Thank you
but i get one string: $full = $dollars.'.'.$cents;
How can i convert this string to money, because i want to save his value in mysql table?
Thank you
Just insert it into MySQL without quotes around it in your query.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply