4

I'm trying to set precision (i.e. number of decimal places) of floats and long doubles to 10, but i don't want them zero padded. I.e.,

123456.789123456789 should give 123456.7891234568, but 123456 should not give 123456.0000000000, but rather 123456

So far i've narrowed it down to:

long double myNumber;
string myString;
ostringstream myStream;
myStream.setf(ios::fixed,ios::floatfield);
myStream.precision(10);
myStream << myNumber;
myString = myStream.str();

I've also tried fiddling with setfill(' ') and std::ws but can't really get the hang of it. Any suggestions?

1 Answer 1

2

Don't use ios::fixed for ios::floatfield. That's what causes the padding. Simply use setprecision.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.