473,387 Members | 1,590 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,387 software developers and data experts.

Combining fixed and scientific in stringstream

Hello group,

I've a simple problem with the precision specifiers of stringstream.
Let's say I have this:

double x = 123.4567890;
std::stringstream Strm;
Strm.precision(4);
Strm << std::scientific << x;
std::cerr << Strm.str() << std::endl;

It will display "123.4". Now when I change x to be 1, it will display
"1". However, in that case I'd prefer if it could display "1.000", i.e.
not making the precision field the *minimum* precision but always the
exact displayed precision.

When I change to "fixed" I get the desired behavior - somehow, at least:
it only affects the digits after the decimal point, which is also what I
do not want. To sum it all up, here is what value yield which
representation and which I'd like:

Value scient fixed Preferred
1 1 1.0000 1.000
12 12 12.0000 12.00
123 123 123.0000 123.0
1.23 1.23 1.2300 1.230
1.234 1.234 1.2340 1.234
12345 1.234E4 12345.0000 1.234E4
10000 1E4 10000.0000 1.000E4

I don't know if this is possible, but I'm happy to hear any suggestions.

Thanks in advance,
Johannes

--
"Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
reicht zu wissen, daß andere es besser können und andere es auch
besser machen um einen Vergleich zu bringen." - Wolfgang Gerber
in de.sci.electronics <47***********************@news.freenet.de>
Jul 23 '08 #1
4 8677
Johannes Bauer schrieb:
To sum it all up, here is what value yield which
representation and which I'd like:
Oh boy, that got messed up. Again, now with spaces instead of tabs
Value scient fixed Preferred
1 1 1.0000 1.000
12 12 12.0000 12.00
123 123 123.0000 123.0
1.23 1.23 1.2300 1.230
1.234 1.234 1.2340 1.234
12345 1.234E4 12345.0000 1.234E4
10000 1E4 10000.0000 1.000E4
Regards,
Johannes

--
"Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
reicht zu wissen, daß andere es besser können und andere es auch
besser machen um einen Vergleich zu bringen." - Wolfgang Gerber
in de.sci.electronics <47***********************@news.freenet.de>
Jul 23 '08 #2
Johannes Bauer wrote:
Johannes Bauer schrieb:
>To sum it all up, here is what value yield which representation and
which I'd like:

Oh boy, that got messed up. Again, now with spaces instead of tabs
>Value scient fixed Preferred
1 1 1.0000 1.000
12 12 12.0000 12.00
123 123 123.0000 123.0
1.23 1.23 1.2300 1.230
1.234 1.234 1.2340 1.234
12345 1.234E4 12345.0000 1.234E4
10000 1E4 10000.0000 1.000E4
I don't know of any trick to format the
exponent in %g-mode, so this is what I have:

...
stringstream s;

s.setf(ios::fixed, ios::floatfield);
s << setw(5) << setprecision(3) << 1. << endl;
s << setw(5) << setprecision(2) << 12. << endl;
s << setw(5) << setprecision(1) << 123. << endl;
s << setw(5) << setprecision(3) << 1.23 << endl;
s << setw(5) << setprecision(3) << 1.234 << endl;

s.setf(ios::scientific, ios::floatfield);
s << setw(5) << setprecision(3) << 12345. << endl;
s << setw(5) << setprecision(3) << 10000. << endl;
...

An yes, Posix printf/sprintf is really powerful.
What's missing in std is a printf-conformal:
ostream& format(ostream&, "printf-format", value list)
or something like that.

Regards

M.
Jul 23 '08 #3
Mirco Wahab schrieb:
I don't know of any trick to format the
exponent in %g-mode, so this is what I have:
[...]
s << setw(5) << setprecision(3) << 1. << endl;
s << setw(5) << setprecision(2) << 12. << endl;
s << setw(5) << setprecision(1) << 123. << endl;
Well, yes, the output looks right - but the "setprecision" part will be
a pain to implement, probably... I just thought maybe C++ provided some
powerful means of formatting those values.
An yes, Posix printf/sprintf is really powerful.
What's missing in std is a printf-conformal:
ostream& format(ostream&, "printf-format", value list)
or something like that.
Yes, it is very much indeed :-(

printf is probably the thing I miss most when using C++. There it would
be a mere "%.3lf" and the thing would be done. Thing is, it's not
typesafe. If there were some way to gernerally format strings, that'd be
quite nice. I don't want to rely on sprintf with a C++ project though, I
have my doubts... I'll see what I come up with.

Thanks for your help,
Johannes

--
"Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
reicht zu wissen, daß andere es besser können und andere es auch
besser machen um einen Vergleich zu bringen." - Wolfgang Gerber
in de.sci.electronics <47***********************@news.freenet.de>
Jul 23 '08 #4
On Jul 23, 11:24 am, Johannes Bauer <dfnsonfsdu...@gmx.dewrote:
Mirco Wahab schrieb:
I don't know of any trick to format the exponent in %g-mode,
so this is what I have:
In general, you can't format the exponent, neither in %g nor in
%e (and nor with ostream nor with printf).
[...]
s << setw(5) << setprecision(3) << 1. << endl;
s << setw(5) << setprecision(2) << 12. << endl;
s << setw(5) << setprecision(1) << 123. << endl;
Well, yes, the output looks right - but the "setprecision"
part will be a pain to implement, probably... I just thought
maybe C++ provided some powerful means of formatting those
values.
The problem, really is that you want the formatting to depend
(at least partially) on the value. Neither C (printf) nor C++
really support this.

You might try setting showpoint, however. According to the
standard, this should be the equivalent of using the '#' flag in
printf, which forces trailing zeros in %g. So despite its name,
it should work for that, and does with g++. But you'll still
not get E4 when the %e format is used: the standards (both C and
C++) require E+04 (i.e. the sign, even if positive, and exactly
two digits, if it fits).
An yes, Posix printf/sprintf is really powerful.
Funny, I never noticed that. The iostream formatters are a lot
more powerful, at least in terms of what you can do. And if
they don't suffice, of course, you just write a wrapper class,
and define << and >for it.
What's
missing in std is a printf-conformal:
ostream& format(ostream&, "printf-format", value list)
or something like that.
Yes, it is very much indeed :-(
printf is probably the thing I miss most when using C++. There
it would be a mere "%.3lf"
That won't get the format you were requesting. What you were
requesting was basically '%#.4g" (but with a different format
for the exponent, if scientific format was used---which isn't
supported).

More generally, of course, you don't really want to specify such
details at the point of the output statement. You want to
specify what the value means, semantically, and then somewhere
globally (e.g. like in a style sheet) specify how such semantic
values are formatted. You can sort of do this with printf:

printf( ("some text " + semanticFormat + " so on).c_str(), ... ) ;

but it hardly seems natural. Where as iostream was designed
precisely with this in mind:

std::cout << "some text " << semanticFormat << value << " so
on" ...

The whole point of manipulators is that you write your own, for
your programs semantics, so that you don't have to specify the
physical details of formatting many times over, every time you
output a value representing the given semantics.
and the thing would be done. Thing
is, it's not typesafe.
And can't be generalized (i.e. to output to a socket, or with a
back end filter that inserts time stamps, or expands tabs). And
you more or less have to embed the physical formatting
information in with the normal text, which is totally wrong from
a software engineering point of view.
If there were some way to gernerally format strings, that'd be
quite nice. I don't want to rely on sprintf with a C++ project
though, I have my doubts... I'll see what I come up with.
You might want to have a look at Boost format. I've not had the
occasion to try it out myself, but from discussions with the
author, I think that it allows the use of manipulators for
format specifiers, but still allows keeping the text in a single
block (important for translation purposes).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 23 '08 #5

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

Similar topics

7
by: frizzle | last post by:
Hi, I know this might sound strange but i think(/hope) it's quite simple: I'm running 2 queries in a mysql DB, first one returns 20 results. Now how can i echo results from the second query...
2
by: Bill Beacom | last post by:
Hi all, I am trying to use stringstream to assemble a text message from a set of user-defined objects that have the insertion operator overloaded. It seems to be putting them into the...
2
by: Woodster | last post by:
I am using the std::setprecision function to format variables of type double in a string however I am unsure how to stop this appearing in scientific notation. For example std::stringstream...
8
by: mikea_59 | last post by:
I am having trouble combining similar elements. Elements can be combined if they have the same name and the same attribute values. I can handle single level elements but am having problems with...
7
by: klaus hoffmann | last post by:
Is it possible to convert 2 characters from a stringstream to an integer without using an intermediate a 2-bytes string ? The following fragment doesn't work #include <iostream> #include...
5
by: cherico | last post by:
I'd like to read stings from cin and put them in stringstream. I use a string object as an intermediate "container" to store data from cin and then put them in stringstream. stringstream ss ;...
1
by: Dave | last post by:
Hello all, One I switch a stream to scientific or fixed mode, how do I turn that setting off? Thanks, Dave
7
by: Barry | last post by:
Hi all, I've noticed a strange error on my website. When I print a capital letter P with a dot above, using & #7766; it appears correctly, but when I use P& #0775 it doesn't. The following...
1
by: kathy | last post by:
I have code: std::stringstream sStream; double pi = 3.14159; .... sStream << pi; //????????????? std::str = sStream.str(); //I want str show the scientific format.
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.