Change 4 to 4.00 and 2.5 to 2.50?

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
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

Change 4 to 4.00 and 2.5 to 2.50?

Post by SmokyBarnable »

I want to change a value I get from my datatbase from, for example, 4 to 4.00 or 2.5 to 2.50 because I am inserting it into a string where I want to print $4.00 or $2.50. Is there a good way to do this using php?

Thanks for any help.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
Ind007
Forum Newbie
Posts: 14
Joined: Fri Dec 01, 2006 12:39 am
Location: India

Post by Ind007 »

simple..

Code: Select all

if(is_int($num))
{
	$num_str="$".$num.".00";

}
elseif(is_float($num))
{
	$num_str="$".$num."0";
}
echo $num_str;
here $num is the value you get from database and I assumed that ,you get the values from database x, x.y format only.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

MySQL will help you out after you insert if the column type is set to

Code: Select all

DECIMAL(10,2)
Where the first number is the length of the numbers to the left of the decimal, and the second, the length of the numbers to the right of the decimal.
Post Reply