473,396 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How do I format this floating point number in a line?

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, -

Mar 20 '06 #1
2 1507
number_format() function might help you.

Mar 20 '06 #2
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-

Mar 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
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...
6
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.
5
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...
23
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...
15
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...
5
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...
32
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 );...
18
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...
3
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')
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.