std::setprecision

T

tarmat

I'm trying to create a function that will turn a float into a
std::string that always shows the number to two decimal places. I have
the following but it doesn't give the desired output:


inline std::string ftos(float f)
{
std::eek:stringstream buffer;

buffer << std::setprecision(2) << f;

return buffer.str();
}

for example

ftos (34.22) gives "34"
ftos(3.123) gives "3.1"

how do I create a function that will simply chop of all the digits
after the second digit from the decimal point?

thanks
 
T

tom_usenet

inline std::string ftos(float f)
{
std::eek:stringstream buffer;

buffer << std::setprecision(2) << f;

buffer << std::fixed << std::setprecision(2) << f;

Tom
 
F

Fraser Ross

I don't see what happened. Isn't normal notation the default? How many
notations is there?

Fraser.
 
F

Fraser Ross

I see those numbers did not need a E in them. Scientific notation is the
default.

Fraser.
 
T

tom_usenet

I don't see what happened. Isn't normal notation the default? How many
notations is there?

3 main ones I think: normal, scientific and fixed precision.

Tom
 
J

Jerry Coffin

"Fraser Ross" said:
I don't see what happened. Isn't normal notation the default? How many
notations is there?

In C there are three conversions: %e, %f and %g. By default, ostreams
use the equivalent of %g, which automatically chooses between %e and %f.
For ostreams, these are equivalent to scientific and fixed respectively.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top