problem with double precision *.dat file

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 ?
You could convert it to a string and remove the extra data that way.
Um, yeah, on second thought, use that, heh. Much better.
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);


Better to use setpercision if you are using C++.
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.