|
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-
Navigation:
[Reply to this message]
|