Reply to Re: How do I format this floating point number in a line?

Your name:

Reply:


Posted by David Haynes on 03/20/06 22:32

laredotornado@zipmail.com wrote:
> Hi,
>
> I am grabbing prices from a database. Sometimes the price is NULL and
> sometimes there's a floating point value. Using PHP 4, when I print
> the price to my CSV file, I want the price, rounded to the second
> decimal place if the pirce is non-empty, but nothing if the price was
> empty.
>
> This doesn't work (it prints out 0.00 if the price is empty), but right
> now I have
>
> $line =
> sprintf("\"%s\",\"%s\",\"%s\",\"%s\",\$%2.2f,\$%2.2f,\"%s\",%d,\"%s\"\r\n",
> $company,
> $model_num,
> $box_name,
> $compatible_models,
> $retail_price,
> $price,
> $description,
> $max_yield,
> $color);
>
> How do I adjust the above to get my desired result?
>
> Thanks, -
>
If you want to keep most of what you have:

$price_str = sprintf("\$%2.2f", $price);
$price_str = ($price_str == '$0.00') ? '' : $price_str;

Then change your \$2.2f to %s and $price to $price_str.

-david-

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация