Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decimal points with std::stringstream?

I have a bunch of integers that I put into stringstreams. Now I want to change the stringstreams into strings while keeping a constant precision with the strings. How would I do that? I know I can use stringstreams.precision(), but it's not working for some reason:

float a = 5.23; float b = 3.134; float c = 3.0;  std::stringstream ta; std::stringstream tb; std::stringstream tc;  ta << a; tb << b; tc << c;  ta.precision(2); tb.precision(2); tc.precision(2);  std::string out = ""; out += ta.str() + "\n"; out += tb.str() + "\n"; out += tc.str() + "\n"; 

Will return 5.23\n3.134\n3.0, rather than 5.23\n3.13\n3.00

like image 656
noobcpp Avatar asked Feb 25 '11 03:02

noobcpp


People also ask

How many decimals is Float32?

Details on floating-point numbers are in Appendix XXXX. Julia has 16-,32- and 64-bit floating point numbers called Float16 , Float32 and Float64 and by default on most systems is the Float64 . Float16 stores 4 decimal digits and the max is about 32,000. Float32 stores 8 decimal digits and the max is about \(10^{38}\).

How do you set precision?

By using the setprecision function, we can get the desired precise value of a floating-point or a double value by providing the exact number of decimal places. If an argument n is passed to the setprecision() function, then it will give n significant digits of the number without losing any information.

Why do we use Stringstream?

The stringstream class in C++ allows a string object to be treated as a stream. It is used to operate on strings. By treating the strings as streams we can perform extraction and insertion operation from/to string just like cin and cout streams.

Why do we use Stringstream in C++?

The StringStream class in C++ is derived from the iostream class. Similar to other stream-based classes, StringStream in C++ allows performing insertion, extraction, and other operations. It is commonly used in parsing inputs and converting strings to numbers, and vice-versa.


1 Answers

I think that your problem is that precision() sets the precision used in future stream insertion operations, not when generating the final string to present. That is, by writing

ta << a; tb << b; tc << c;  ta.precision(2); tb.precision(2); tc.precision(2); 

You're setting precision too late, as the first three lines have already converted the floating point numbers to strings using the default precision.

To fix this, try changing the order in which you execute these statements to

ta.precision(2); tb.precision(2); tc.precision(2);  ta << a; tb << b; tc << c; 

This will cause the writes into the stringstream to use your custom precision rather than the existing defaults.

However, the effect of the precision modifier is only meaningful if you explicitly tell the stream that you want to use either fixed-precision or scientific notation for output. To do this, you can use either the fixed or scientific modifiers:

ta.precision(2); tb.precision(2); tc.precision(2);  ta << fixed << a; tb << fixed << b; tc << fixed << c; 

This will correctly display the appropriate number of digits.

On a related note, you don't need to use three stringstreams to accomplish your goal. You can just use one:

std::stringstream t; t.precision(2);  t << fixed << a << '\n' << b << '\n' << c << '\n';  std::string out = t.str(); 
like image 109
templatetypedef Avatar answered Sep 19 '22 05:09

templatetypedef