With the following 2 functions focus on the rows with the * comments. When the cout<< statement executes, there is no decimal place showing. 3021680380 /10000000 turns into 302. instead of 302.1680.
void convert(){
setprecision(4); //************************
newFileTime = new double[numRec]; //***********
newOffset = new int[numRec];
newSize = new int[numRec];
newNum = new int[numRec];
newType = new int[numRec];
newTime = new int[numRec];
for(int i = 0; i<numRec; i++){
newFileTime[i] = fileTime[i] / 10000000; //**********
newOffset[i] = offset[i] / 512;
newSize[i] = fileSize[i] / 512;
newNum[i] = 0;
if(type[i] == "Write"){
newType[i] = 0;
}else{
newType[i] = 1;
}
newTime[i] = responseTime[i] / 10000000;
}
}
void exports(){
setprecision(4); //**************
ofstream fout;
fout.open("prxy_0.ascii");
{
if(!fout){
cout <<"File opening failed. \n";
}
}
fout<< numRec <<endl;
for(int i = 0; i < numRec; i++){
fout<< newFileTime[i] << " " << newNum[i] << " " << newOffset[i] << " " << newSize[i] << " " << newType[i] << " " << newTime[i];
cout<< fileTime[i] << " " << newFileTime[i] <<endl; //**********
if(i != numRec - 1){
fout<<endl;
}
}
fout.close();
}
Any ideas?
Whenever you divide two integers, the result of this expression also is an integer. Integer division is always rounded down.
To fix your expression, cast one of the operands to a
doubleto make the division a floating point division (use one of the following possibilities):Please note that setting the precission requires you to put the call of
std::setprecision(...)into the stream for which you want to set the precision. Also, this only sets the output precision (when writing to the stream), not how the calculations are performed:Also note that
std::setprecisionsets the number of magnificant digets rather than the decimals after the decimal point. So your302.1680would be printed as302.2(four maginificant digits). To set a fixed number of digits after the decimal point, also writestd::fixedto the stream, either before or afterstd::setprecision:Note that such configurations will be kept during the runtime of your program until you change them again. To keep them local in a function, make sure that you restore the configuration after you’re done.
Of course,
std::coutwas only an exemplary stream. The same applies for writing to files (anystd::ostreamobject).