473,654 Members | 3,060 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::stringstre am 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.electron ics <47************ ***********@new s.freenet.de>
Jul 23 '08 #1
4 8701
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.electron ics <47************ ***********@new s.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::fix ed, 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::sci entific, 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 "setprecisi on" 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.electron ics <47************ ***********@new s.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 "setprecisi on"
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 objektorientier ter Datenverarbeitu ng
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
2789
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 in the 1st query's result, like:
2
5851
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 stringstream fine....however, I need to assemble it in "parts" (eg a prefix, a header, a body, a trailer and a checksum), and would like to reuse the stringstream to format that various parts....but I ahve not found a successful way to "empty" the...
2
8715
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 buffer; buffer << setprecision(1) << 40.0 << "° C";
8
4162
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 multi-level elments (level of element nesting is unbound). I cannot rely on fixed element names in the translator code since the translator will be general purpose, elements names are not fixed. Example input <test>
7
3872
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 <fstream> #include <sstream> #include <iomanip> #include <string> using namespace std;
5
10872
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 ; string str ; cin >> str ; ss << str ;
1
1502
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
4804
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 capital letters all work correctly - B C D F G M S T with the diacritical marker &#_0775. Why am I having a problem with P?
1
2000
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
8294
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,...
1
8494
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,...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
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();...
0
4150
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...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2719
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1597
bsmnconsultancy
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...

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.