Double Length

Aug 3, 2011 at 1:43pm
I have a double read from an input file. I'm not sure if it is bigger than 10 or less than 10, but I always want it to come out as five characters (including the decimal). So, if it is 10.321789, I want to truncate it to 10.32. But if it is 9.98765, I want to truncate it to 9.987 (or 9.988 if possible). Is there an easy way to do this, without checking if the double is >= 10?

Thanks.
Aug 3, 2011 at 1:45pm
In addition, I'm converting these doubles to strings, so maybe I could cut of the string length...
Aug 3, 2011 at 1:49pm
closed account (zwA4jE8b)
You can use a string stream to convert any number into a string.
Then you will just have to resize your string :)

Although, that will not help you with rounding.
try using setprecision()
Last edited on Aug 3, 2011 at 1:50pm
Aug 3, 2011 at 2:05pm
How do I resize a string? I'm not too concerned with rounding.

I thought setprecision() was only good for output.
Aug 3, 2011 at 2:26pm
Nevermind, I figured it out. Thanks for the suggestion.
Aug 4, 2011 at 1:19am
What was your solution??
Last edited on Aug 4, 2011 at 1:19am
Aug 4, 2011 at 8:05am
i can't understand what are you mean ! but i think your solution is precision for all of number or fixed for just set in number of after point :-??
Topic archived. No new replies allowed.