problem with double precision *.dat file

Jan 27, 2010 at 5:14pm
Hi,
I am trying to create a ".dat" file in which I want to save the numbers in double precision with desired number of digits after decimal.
for example:
suppose I have the number 2.34567890, and I want to save it up to 2.345 i.e. to control the size of my variable type.

Could you just tell me the proper way for this ?
Jan 27, 2010 at 5:31pm
You could convert it to a string and remove the extra data that way.
Jan 27, 2010 at 5:33pm
Jan 27, 2010 at 5:41pm
Um, yeah, on second thought, use that, heh. Much better.
Jan 28, 2010 at 5:16am
Use sprintf. You can specify the precision there, but be careful the precision of double numbers on other factors as well, but if you are intending to use till 14 or 16 digits after decimal, you can go ahead with sprintf.


char buffer[1024];

double number=2.34567890

sprintf(buffer, "%.14f",number);

or

sprintf(buffer,"%.14g", number);


Jan 28, 2010 at 5:59am
Better to use setpercision if you are using C++.
Jan 28, 2010 at 6:29am
Thanks a lot to everyone. The use of manipulators that Bazzy suggested was the same which I desired to get.
Topic archived. No new replies allowed.