Hello all,
The application I am working on must never output numbers in a
floating-point format, that is, something like
2e-002
is a big no-no. At the same time, it must output numbers in a compact way,
that is, it should only output significant digits, and only output a decimal
point if there are digits following it.
There seems to be no printf modifer to accomodate me.
%f is fixed-point but not compact:
printf("%f", 222) produces 222.000000 and I only need 222
%g is compact but not fixed-point:
printf("%g", 0.00001) produces 1e-005 and I want 0.000001
Any way I can do this with printf alone without writing my own 0-trimming
code?
TIA.
Abe 5 6763
Thanks for a fast reply. I can write 0-trimming code, no problem, I was just
hoping for a cleaner solution based solely on printf's capabilities.
--
abe
"ark" <ar****@comcast .net> wrote in message
news:xU******** **********@rwcr nsc52.ops.asp.a tt.net... "Abe Simpson" <ab*@simpson.co m> wrote in message news:TF******** ************@tw ister.nyc.rr.co m... Hello all,
The application I am working on must never output numbers in a floating-point format, that is, something like
2e-002
is a big no-no. At the same time, it must output numbers in a compact
way, that is, it should only output significant digits, and only output a decimal point if there are digits following it.
There seems to be no printf modifer to accomodate me.
%f is fixed-point but not compact:
printf("%f", 222) produces 222.000000 and I only need 222
%g is compact but not fixed-point:
printf("%g", 0.00001) produces 1e-005 and I want 0.000001
Any way I can do this with printf alone without writing my own
0-trimming code?
TIA.
Abe
Try sprintf your number with a proper %f format to a small buffer and then char *p = strchr(buffer, '.'). p-buffer tells you how many digits before
the decimal point there are and add however many more positions you are
allowed. Write '\0' there and output the string. arkk at macroexpression s.com
Abe Simpson wrote: Hello all,
The application I am working on must never output numbers in a floating-point format, that is, something like
2e-002
is a big no-no. At the same time, it must output numbers in a compact way, that is, it should only output significant digits, and only output a decimal point if there are digits following it.
There seems to be no printf modifer to accomodate me.
My advice would be to not use printf at all. Since you posted to a C++
group, I will assume you are using C++ and recommend type-safe iostream
classes instead. %f is fixed-point but not compact:
printf("%f", 222) produces 222.000000 and I only need 222
Actually, this produces undefined behavior because you pass an int where
the format string indicates it will be a double. I really doubt you got
this to produce 222.000000. This is a nice example of why most people
should not use printf, scanf, or any function that defeats type-checking
(if it can be avoided). %g is compact but not fixed-point:
printf("%g", 0.00001) produces 1e-005 and I want 0.000001
Any way I can do this with printf alone without writing my own 0-trimming code?
I don't see a way to do it with printf alone.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Abe Simpson wrote: Hello all,
The application I am working on must never output numbers in a floating-point format, that is, something like
2e-002
is a big no-no. At the same time, it must output numbers in a compact way, that is, it should only output significant digits, and only output a decimal point if there are digits following it.
There seems to be no printf modifer to accomodate me.
%f is fixed-point but not compact:
printf("%f", 222) produces 222.000000 and I only need 222
Nit-pick: printf("%f", 222) produces undefined behavior,
because `222' is not a `double' quantity as required by "%f".
%g is compact but not fixed-point:
printf("%g", 0.00001) produces 1e-005 and I want 0.000001
Any way I can do this with printf alone without writing my own 0-trimming code?
You need to decide how many digits to the right of the
decimal point you want. This will depend on the size of the
number (the log10() function may come in handy here) and on
your notion of how many digits are "significan t." Armed with
this information you can then choose an appropriate precision
for the conversion, which you'll probably write as
printf ("%.*f", precision, value);
As an aside, note that printing values with very large or
very small magnitudes is not especially "compact."
-1e37 = -100000000000000 000000000000000 00000000
1e-37 = 0.0000000000000 000000000000000 000000001
.... and you don't even want to *think* about 1e308 ...
-- Er*********@sun .com
Eric Sosman wrote:
Nit-pick: printf("%f", 222) produces undefined behavior, because `222' is not a `double' quantity as required by "%f".
I find it strange that you consider this nit-picking. This is pretty
serious case of undefined behavior, likely leading to a program crash,
and maybe even compromising security.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Kevin Goodsell wrote: Eric Sosman wrote:
Nit-pick: printf("%f", 222) produces undefined behavior, because `222' is not a `double' quantity as required by "%f".
I find it strange that you consider this nit-picking. This is pretty serious case of undefined behavior, likely leading to a program crash, and maybe even compromising security.
In actual quoted code I'd have considered it more serious.
But it seemed to me that the O.P. had just typed the code in
without serious consideration -- for example, note the lack of
a newline character, and of a semicolon -- and was concentrating
on the issue of how to control the format of a converted `double'.
So I noted the problem in passing, but didn't spend many electrons
on it. You're entirely right, though: a mismatch like this can
be a serious matter. At least once a fortnight I'm grateful when
gcc prevents me from making just such an error ...
-- Er*********@sun .com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Adrian Parker |
last post by:
I'm using the code below in my project. When I print all of these fixed
length string variables, one per line, they strings in questions do not
properly pad with 0s. strQuantity prints as " 4". Six spaces than the
value of intQuantity. This is correct.
But all the others end up being string objects of only 6 characters long
(with the exception of strTotal). The left most positions of the string
object are being padded with one...
|
by: Jaz |
last post by:
Trying to use a fixed layer for a couple of NAV buttons.
I found this code, but the IE part is commented, and I don't
understand the IF statement. It works great on Moz/Firebird and Opera
BUT not IE. Would someone please take a look at this and tell me if it
can made to work in IE? If it can, a hint on the syntax/code would be
much appreciated! PS, this would also get rid of my fixed background.
Thanks in advance
Jaz
|
by: SoloCDM |
last post by:
How do I keep my entire web page at a fixed width?
*********************************************************************
Signed,
SoloCDM
|
by: Paul Trautwein |
last post by:
I'm trying to get an image to float in a window despite scrolling.
I've gotten it to work on my Mac using IE 5.2, Netscape, and Safari, but it
goes wonky when I test it on a PC. (testing with IE only at the moment.)
Positioning is wrong, and it doesn't float at all.
Here's a test page: http://www.bdiusa.com/mirrors/test.html
I've tested the code using the CSS Validator on the W3 site - and it said
it's okay.
|
by: Mason A. Clark |
last post by:
Masters:
On two or three-column layouts, one column often has a list
of links. Scrolling the page hides them. I'm aware there's
supposed to be the ability to fix the column (frame-like).
I have some bits of such code but haven't yet made it
work well.
Question: Why have I never seen an example on the web?
Not that I've seen everything, but I've seen numerous pages
| |
by: Otie |
last post by:
Hello,
I am using the MSFlexGrd Control in VB5. I have 1 fixed row and one fixed
column. I am trying to do a sort when the user clicks a column in the FIXED
ROW. But when I capture the row number in the click event I get row = 1 if I
click on the FIXED row OR the actual row 1. How can I get the grid control
to tell me when the user has clicked on the FIXED row and not on row 1
(since they both produce row = 1 and I cannot tell them apart...
|
by: O.B. |
last post by:
In the example below, I'm trying to convert a fixed byte array to a
string. I get an error about needing to use "fixed" but I have no clue
where to apply it. Help?
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
|
by: Rick Knospler |
last post by:
I am trying to convert a vb6 project to vb.net. The conversion worked for
the most part except for the fixed length strings and fixed length string
arrays. Bascially the vb6 programmer stored all form data in a fixed length
structure that is written direct to disk.
I need to load the existing files, into a fixed length structure to
initialize the form data within the project.
I am running into problems with statements like the...
|
by: Jeff |
last post by:
Hey
I'm wondering how the Fixed-Width Text Format is
What I know is that the top line in this text format will contain column
names. and each row beneath the top line represent for example a row in a
table etc...
But does fixed-with mean that every column has a fixed with: for example
first column is 10 char wide and second column is 30 char wide?
|
by: ingejg |
last post by:
I am starting to study internet synchronization, and my head is still
spinning since internet is not my forte, however my boss is breathing
down my neck at the moment. Our company has only one server with fixed
IP address provided by our ISP, while the other sites (which I wish in
the future hold the replicas databases) have only standard internet
connections with Dynamic IP (which means that they change IP
addresses, as given by the ISP...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |