Rounding to integer when converting from string to double

Dear All,

I made data from the *.csv file as string read, and then made them write to *.txt file as string with precise values!
Now I want to convert the strings in text file with precise values to double. Here is the problem which is rounding them to integer numbers by atof(), istringstream() … etc.

I didn’t understand the reason why they couldn’t be converted to '‘double’'s with precise values !

PS : *.text and *.csv files are in the attachment.

#include <sstream>
#include <string>
#include <fstream>

using namespace std;

void readfromCSV()
{

  string serial, Sks,SKBs,SP500s,SP800s,SP900s,QE350s,QE400s,EBBs,IDBs;
  double Sk,SKB,SP500,SP800,SP900,QE350,QE400,EBB,IDB;
  double d;
  char *pEnd;

  ofstream out1;
  out1.open("outtext.txt");

  ofstream out;
  out.open("HamamatsuInfo.txt",ios::out);

  ifstream infile("Hamamatsu_R7600PMT_list.csv"); // for example                                                                                                                   
  string line;

  while (getline(infile, line)){

    stringstream strstr(line);

    string word;
    double number;

    while(getline(strstr,word,';')) { out<<setprecision(10)<<word<<'\t'; }
  }

  ifstream input;
  input.open("HamamatsuInfo.txt",ios::in);

  while(input>>serial>>Sks>>SKBs>>SP500s>>SP800s>>SP900s>>QE350s>>QE400s>>EBBs>>IDBs) {

    //    cout<<serial<<Sks<<SKBs<<SP500s<<SP800s<<SP900s<<QE350s<<QE400s<<EBBs<<IDBs<<endl;                                                                                       

    IDB=atof(IDBs.c_str());
    cout.precision(10);
    cout<<IDBs<<"__"<<IDB<<endl;

    /*                                                                                                                                                                             
    istringstream(Sks)>>Sk;                                                                                                                                                        
    istringstream(SKBs)>>SKB;                                                                                                                                                      
    istringstream(SP500s)>>SP500;                                                                                                                                                  
    istringstream(SP800s)>>SP800;                                                                                                                                                  
    istringstream(SP900s)>>SP900;                                                                                                                                                  
    istringstream(QE350s)>>QE350;                                                                                                                                                  
    istringstream(QE400s)>>QE400;           
    istringstream(EBBs)>>EBB;                                                                                                                                                      
    istringstream(IDBs)>>IDB;                                                                                                                                                      
    */

  }
}

Cheers,
Ersel
HamamatsuInfo.txt (82.5 KB)
Hamamatsu_R7600PMT_list.csv (82.5 KB)

as said here:

cplusplus.com/reference/ioma … precision/

“setprecision sets the decimal precision to be used to format floating-point values on output operations.”

That does not apply on string …

Your “.csv” file is “broken” (originates from a non-English-locale system).
You should at least fix “decimal separators”:
sed -i -e ‘s/,/./g’ Hamamatsu_R7600PMT_list.csv
But in general, a “.csv” file (i.e. a “Comma-Separated Values” file) should be formatted like this (“interchangeable” English-locale):
sed -i -e ‘s/,/./g;s/;/,/g’ Hamamatsu_R7600PMT_list.csv

[quote=“couet”]as said here:

cplusplus.com/reference/ioma … precision/

“setprecision sets the decimal precision to be used to format floating-point values on output operations.”

That does not apply on string …[/quote]

Ok I understood that and removed it. Thanks Qlivier.

[quote=“Pepe Le Pew”]Your “.csv” file is “broken” (originates from a non-English-locale system).
You should at least fix “decimal separators”:
sed -i -e ‘s/,/./g’ Hamamatsu_R7600PMT_list.csv
But in general, a “.csv” file (i.e. a “Comma-Separated Values” file) should be formatted like this (“interchangeable” English-locale):
sed -i -e ‘s/,/./g;s/;/,/g’ Hamamatsu_R7600PMT_list.csv[/quote]

I created new *.csv file as ‘‘comma-separated values’’ file again, and implemented it this code ‘‘sed -i -e ‘s/,/./g;s/;/,/g’ Hamamatsu_R7600PMT_list.csv’’ . By the way, the code produces one more *.csv-e file. Anyway, because data in *.csv file have "comma"s, I did the new script with this code “while(getline(strstr,word,’,’))” instead of “while(getline(strstr,word,’;’))” and the script works very well !
Done!
Thank you very much Pepe =D>


#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
using namespace std;

void readfromCSV(){

  string serial, Sks,SKBs,SP500s,SP800s,SP900s,QE350s,QE400s,EBBs,IDBs;
  double Sk,SKB,SP500,SP800,SP900,QE350,QE400,EBB,IDB;

  ofstream out1;
  out1.open("outtext.txt");

  ofstream out;
  out.open("HamamatsuInfo.txt",ios::out);

  ifstream infile("Hamamatsu_R7600PMT_list.csv"); // for example                                                                                                                   
  string line;

  while (getline(infile, line)){

    stringstream strstr(line);

    string word;
    while(getline(strstr,word,',')) { out<<word<<'\t'; }
  }

  ifstream input;
  input.open("HamamatsuInfo.txt",ios::in);

  while(input>>serial>>Sks>>SKBs>>SP500s>>SP800s>>SP900s>>QE350s>>QE400s>>EBBs>>IDBs) {

    QE400=atof(QE400s.c_str());
    QE350=atof(QE350s.c_str());
    SKB=atof(SKBs.c_str());
    Sk=atof(Sks.c_str());
    SP500=atof(SP500s.c_str());
    SP800=atof(SP800s.c_str());
    SP900=atof(SP900s.c_str());
    EBB=atof(EBBs.c_str());
    IDB=atof(IDBs.c_str());

    out1<<serial<<setw(7)<<Sk<<setw(7)<<SKB<<setw(7)<<SP500<<setw(7)<<SP800<<setw(7)<<SP900<<setw(7)<<QE350<<setw(7)<<QE400<<setw(7)<<EBB<<setw(7)<<IDB<<endl;

  double IDBS = IDB*2;
  cout<<IDBS<<endl;
}
}

HamamatsuInfo.txt (88.7 KB)
Hamamatsu_R7600PMT_list.csv (88.7 KB)