Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 how to put a real number into a file
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
liangchenen




PostPosted: 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++?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Mon Aug 08, 2005 9:09 pm   Post subject: (No 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;
}
liangchenen




PostPosted: Mon Aug 08, 2005 10:27 pm   Post subject: (No 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.
wtd




PostPosted: Mon Aug 08, 2005 11:41 pm   Post subject: (No 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;
liangchenen




PostPosted: Tue Aug 09, 2005 11:04 am   Post subject: (No 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?
liangchenen




PostPosted: 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.
wtd




PostPosted: Tue Aug 09, 2005 4:14 pm   Post subject: (No 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.
bugzpodder




PostPosted: Sat Aug 13, 2005 6:15 pm   Post subject: (No subject)

how about we do it the C++ way?

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




PostPosted: Wed Aug 24, 2005 12:05 pm   Post subject: (No 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?
wtd




PostPosted: Wed Aug 24, 2005 1:06 pm   Post subject: (No 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.
bugzpodder




PostPosted: Wed Aug 24, 2005 10:34 pm   Post subject: (No 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;
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: