Getting 2 decimal places in output text file
Jul 30, 2014 at 3:36pm UTC
Hello everyone. I was wondering how I can make my output only have 2 decimal places because I want it to be a monetary value. In my code I used this:
1 2 3
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
which worked well at first because on my command prompt screen the numbers had two decimal places and looked liked this:
1 2 3 4
Premium for Pol1 is $14101.62
Premium for Pol2 is $14221.16
Premium for Pol3 is $582390.50
Premium for Pol4 is $220384.49
However, I also have the program outputting to a textfile called "output.txt" and when I opened this text file the numbers only had one or no decimal places.
1 2 3 4
Premium for Pol1 is $14101.6
Premium for Pol2 is $14221.2
Premium for Pol3 is $582391
Premium for Pol4 is $220384
How come the code for two decimal places is working for my output to command prompt but not my output to the text file?
Here is my code if it helps at all. Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
double ratesmn[86] = {
#include "MaleNonSmoker.txt"
- 1
};
double ratesms[86] = {
#include "MaleSmoker.txt"
- 1
};
double ratesfn[86] = {
#include "FemaleNonsmoker.txt"
- 1
};
double ratesfs[86] = {
#include "FemaleSmoker.txt"
- 1
};
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <math.h>
#include <stdio.h>
using namespace std;
#define STARTAGE 15
int main(int argc, char ** argv) {
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
double tpx[100];
double myrate[100];
double T[100];
double vT[100];
double px[100];
double ax[100];
double const *rates;
string line;
string age, death_ben, interest, end_age;
string policy, gender, smoker_status, db_option;
ifstream datafile("Policies.txt" );
if (datafile.is_open()){
ofstream a_file("output.txt" );
while (!datafile.eof()){
datafile >> policy >> gender >> smoker_status >> age >> death_ben >> db_option >> interest >> end_age;
int Age;
int DeathBen;
double Interest;
int endAge;
if (!(istringstream(age) >> Age)) Age = 0;
if (!(istringstream(death_ben) >> DeathBen)) DeathBen = 0;
if (!(istringstream(interest) >> Interest)) Interest = 0;
if (!(istringstream(end_age) >> endAge)) endAge = 0;
if (gender == "M" ){
if (smoker_status == "S" ){
rates = ratesms;
}
else {
rates = ratesmn;
}
}
else {
if (smoker_status == "S" ) {
rates = ratesfs;
}
else {
rates = ratesfn;
}
}
double sum = 0;
tpx[Age] = 1;
T[Age] = 1;
vT[Age] = 1 / pow(1+Interest, T[Age]);
while ((Age - STARTAGE) < endAge+1 - STARTAGE){
//cout << "tpx for age " << Age << " is " << tpx[Age] << ".\n";
//cout << "vT for age " << Age << " = " << vT[Age] << "\n";
myrate[Age]= rates[Age - STARTAGE];
px[Age] = 1 - rates[Age - STARTAGE];
ax[Age] = myrate[Age] * tpx[Age] * vT[Age];
//cout << "qx for age " << Age << "= " << myrate[Age] << "\n";
//cout << "ax for age " << Age << " is " << ax[Age] << "\n";
Age = Age + 1;
tpx[Age] = tpx[Age - 1] * px[Age - 1];
T[Age] = T[Age - 1] + 1;
vT[Age] = 1 / pow(1 + Interest, T[Age]);
}
int otherAge;
if (!(istringstream(age) >> otherAge)) otherAge = 0;
for (int i = otherAge; i < 100; i++){
sum += ax[i];
}
cout << "Premium for " << policy << " is $" << sum*DeathBen << "\n" ;
a_file << "Premium for " << policy << " is $" << sum*DeathBen << "\n" ;
}
}
printf(" Press enter to exit\n" );
getchar();
return 0;
}
Last edited on Jul 30, 2014 at 3:37pm UTC
Jul 30, 2014 at 4:15pm UTC
You set the precision for cout at line 32.
Where do you set the precision for a_file?
precision is applied on a stream basis. If you want to apply precision for more than one stream, you must call precision for each stream you want it to apply to.
Jul 30, 2014 at 4:36pm UTC
very helpful, I figured it out thank you!
Topic archived. No new replies allowed.