Computer Science Canada

how to put a real number into a file

Author:  liangchenen [ Mon Aug 08, 2005 8:59 pm ]
Post subject:  how to put a real number into a file

if I have a variable mark. and it is 98.123123

and I want to put this real number into a file called "mark.out" with 2 decimals places, can anybody tell me how to do this in C++?

Author:  wtd [ Mon Aug 08, 2005 9:09 pm ]
Post subject: 

c++:
#include <fstream>
#include <iomanip>

int main()
{
   std::ofstream outputFile("mark.out");
   double mark(98.123123);

   outputFile << setprecision(2) << mark << std::endl;

   outputFile.close();

   return 0;
}

Author:  liangchenen [ Mon Aug 08, 2005 10:27 pm ]
Post subject: 

the setprecision part

it gives the significant digits

for example, if my number is 98.41234e1234

and if I have setprecision(3), the output will be 98.4

it is not exactly the decimals.

Author:  wtd [ Mon Aug 08, 2005 11:41 pm ]
Post subject: 

If all else fails, you could truncate the number like so:

c++:
#include <cmath>

double truncate(double number, int digits)
{
   int factor(pow(10, digits));
   return static_cast<double>(static_cast<int>(number * factor)) / factor;
}


c++:
outputFile << truncate(mark, 3) << std::endl;

Author:  liangchenen [ Tue Aug 09, 2005 11:04 am ]
Post subject: 

do you know the following:

printf("%0.2f",mark);

this outputs the real number mark onto the screen with 2 decimals places.

how do I put that into a file?

Author:  liangchenen [ Tue Aug 09, 2005 1:09 pm ]
Post subject:  never mind, I figured it out

cout << setiosflags(ios::fixed) << setprecision(2) << x;

just use this.

use setiosflas(iso::fixed), then it will outputs the decimals to 2 digits instead of the whole thing.

Author:  wtd [ Tue Aug 09, 2005 4:14 pm ]
Post subject: 

liangchenen wrote:
do you know the following:

printf("%0.2f",mark);

this outputs the real number mark onto the screen with 2 decimals places.

how do I put that into a file?


That's C. Avoid it at all costs in C++ code.

Author:  bugzpodder [ Sat Aug 13, 2005 6:15 pm ]
Post subject: 

how about we do it the C++ way?

cout<<setiosflags(ios::fixed)<<setprecision(2)<<myfloat<<endl;

Author:  liangchenen [ Wed Aug 24, 2005 12:05 pm ]
Post subject: 

However the setprecision thing does do the rounding

for example, we have 0.666666..
when I take setprecision 5, it would give me 0.66667

But I do not want it to do the rounding, instead, I want it to trunctate the decimals, what I want is 0.66666

what should I do then?

Author:  wtd [ Wed Aug 24, 2005 1:06 pm ]
Post subject: 

liangchenen wrote:
However the setprecision thing does do the rounding

for example, we have 0.666666..
when I take setprecision 5, it would give me 0.66667

But I do not want it to do the rounding, instead, I want it to trunctate the decimals, what I want is 0.66666

what should I do then?


Write a truncate function that does the work. Here's a thought... if you need it truncated to 2 decimal places, multiply by 100 (10 ^ 2) to move the decimal point two places to the right, cast to int to remove the fractional part, re-cast to a floating point number, then divide by 100.

Author:  bugzpodder [ Wed Aug 24, 2005 10:34 pm ]
Post subject: 

that way would work. but it may cause overflow.
alternatively, use a stringstream and output one more digit than necessary. truncate the last digit of the output.

stringstream ss;
ss<<setiosflags(ios::fixed)<<setprecision(6)<<myValue;
ss>>str;
cout<<str.substr(0,str.length()-1)<<endl;


: