0

I am trying to extract a number from a string and convert it into a double or float so I can do some numerical operations on it. I am able to isolate the variable I need so the string consists only of the number, but when I try to convert it to a float or double it rounds the value, ie from 160430.6 to 160431.

//Helper Function to Extract Value of Interest
//Based on column of final digit of numbers being same across various FLOPS output files
double findValue(string &line, int &refN){
    setprecision(100);
    string output;
    
    //go to end column and work backwards to get value string
    while(line[refN] != ' '){
        output = line[refN] + output;
        refN = refN - 1;    

    }
    
    const char* outputx = output.c_str();
    double out = atof(outputx);
    //removing the const char* line and replacing atof with stod(output) runs into the same issue
    
    return out;
}

int main()
{
    string name;
    cin >> name;
    ifstream file(name);
    //opens file
    if(!file.is_open()){"error while opening the file";
    
    }else{
        //Temporary Reference Definitions
        string ref = "TOGW";
        int refN = 25;
        string line = findLine(file,ref);
        
        double MTOGW = findValue(line, refN);
        cout << MTOGW;
    }
    return 0;
}

I initially tried using stof() to convert, but that rounded. I have also tried using stod() and stold(), and last tried converting to a const char* and using atof(). I have messed with the setprecision() value, but also have not been able to solve it that way.

I cannot use Boost

2
  • A float can only hold about 6 significant digits, so you really need to use double. Don't know why stod would give you problems. Commented Nov 2, 2022 at 4:11
  • C++ does not have a setprecision() function. std::setprecision is an I/O stream manipulator, but you are not using it with any input stream. Also, findValue() can be simplified using std::string::rfind() and std::string::substr() (or std::string_view equivalents), or just a std::istringstream Commented Nov 2, 2022 at 4:13

1 Answer 1

1

You were almost there. The rounding was occurring on output, so that's where you need to use setprecision. That and always use double instead of float to ensure you have enough precision in your variables.

#include <vector>
#include <ranges>
#include <iomanip>
#include <iostream>
#include <string>

using std::string;

double findValue(string &line, int &refN){
    //setprecision(100);
    string output;
    
    //go to end column and work backwards to get value string
    while(line[refN] != ' '){
        output = line[refN] + output;
        refN = refN - 1;    

    }
    
    const char* outputx = output.c_str();
    double out = strtod(outputx, NULL);
    
    return out;
}

int main()
{
    string s = " 160430.6";
    int n = s.size() - 1;
    std::cout << std::setprecision(10) << findValue(s, n) << '\n';
}

See it in action on the Godbolt compiler.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.