Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting of a price (Currency)

Status
Not open for further replies.

gmccammon

IS-IT--Management
Joined
Oct 30, 2006
Messages
1
Location
US
am attempting to print a dynamically generated web page that shows a product and a price. The web page shows 84.9900 as a price. No $, and 4 places to right of decimal. Please look at the last line of code before the comment.

any help would be greatly appreciated.

Code:
$query1 = "SELECT ProductName, ProductDescription, UnitsInStock, UnitPrice FROM Products WHERE ProductID = $productID";
$query_handle = $dbh->prepare($query1);

# Executes the SQL statement defined in query1
$query_handle->execute() or die $dbh->errstr;

# Output database results into an array called @product
while ( @product = $query_handle->fetchrow_array() ) {
# @TODO fill in fields below to correspond with SQL statement (anywhere "###" is)
# Added code to pull the corresponding information from the array based upon the indeces
	# $price = sprintf ("\$ %.2f", $product[3]);
        # format price
 
Yes, the last line of code in your example is pretty much exactly what you need:
Code:
# $price = sprintf ("\$ %.2f", $product[3]);

However, there is one giant flaw in your code. Do you see it? I do. That line of code is commented out. Try uncommenting it, and actually using the "$price" variable in your print statement, as I'm betting that it's currently printing "$product[3]". At least that's my shot in the dark.

Also, you could use a non-interpolated format if you didn't want to have to escape the dollar sign.
Code:
$price = sprintf('$%.2f', $product[3]);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top