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, - 2 1507
number_format() function might help you. la***********@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- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: John Lenton |
last post by:
Is there any reason python's printf-style formatting is missing the
(C99) '%a' specifier?
I'm sorry if this has been asked and answered before; I can't find it
on google ('%a' is a very awkward...
|
by: J |
last post by:
Would anyone know if there a type tag to format a double?
I have f for floating point, but cannot find one for double.
|
by: Subrahmanyam Arya |
last post by:
Hi Folks ,
I am trying to solve the problem of reading the numbers correctly
from a serial line into an intel pentium processor machine .
I am reading 1 byte and 2byte data both positive...
|
by: Matt Garman |
last post by:
Is there a clean, portable way to determine the maximum value of
converted numerical fields with printf()-like functions? Doing this
at compile-time would be preferable.
For example, %i should...
|
by: michael.mcgarry |
last post by:
Hi,
I have a question about floating point precision in C.
What is the minimum distinguishable difference between 2 floating point
numbers? Does this differ for various computers?
Is this...
|
by: sankar |
last post by:
Hi,
I am using a Q14.18 value. There are tables used
in my program which are being indexed by the exponent and mantissa
parts of the corresponding floating point value.
So how can I get the...
|
by: ma740988 |
last post by:
template <class T> inline bool isEqual( const T& a, const T& b,
const T epsilon = std::numeric_limits<T>::epsilon() )
{
const T diff = a - b;
return ( diff <= epsilon ) && ( diff >= -epsilon );...
|
by: Dirk Hagemann |
last post by:
Hello,
From a zone-file of a Microsoft Active Directory integrated DNS server
I get the date/time of the dynamic update entries in a format, which
is as far as I know the hours since january 1st...
|
by: MM |
last post by:
Hi to all,
I'm trying to import a tab separated values file onto Excel with the
following script:
import csv
from pyExcelerator import *
w = Workbook()
worksheet = w.add_sheet('sim1')
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |