0

Im trying to get a float value from a file.txt into a string. When I output that value with std::stof(str) it gets rounded. Example, in the text file there's "101471.71", whet i use the std::stof(str) it returns "101472", how to I avoid this? Here's a part of that code (some parts are in spanish, sorry :p):

double CaptureLine(std::string filepath, int fileline, int linesize)
{
    std::fstream file;
    std::string n_str, num_n;
    int current_line = 0, n_size, filesize = FileSize(filepath);
    char ch_n;
    double n_float = 0.0;
    int n_line = filesize - fileline;
    file.open("registros.txt");
    if (file.is_open()) {
        while (!file.eof()) {
            current_line++;
            std::getline(file, n_str);
            if (current_line == n_line) break;
        }
        if (current_line < n_line) {
            std::cout << "Error" << std::endl;
            return 1;
        }
        file.close();
    }

    n_size = n_str.length();
    for (int i = linesize; i < n_size; i++) {
        ch_n = n_str.at(i);
        num_n.push_back(ch_n);
    }
    std::cout << ">>" << num_n << "<<\n";

    n_float = std::stof(num_n); //Here's the error
    
    return n_float;
}
phuclv
  • 37,963
  • 15
  • 156
  • 475
Levbeux
  • 23
  • 3
  • 2
    in your code you have `stoi` (i.e. to integer) instead of `stof`. – bolov Oct 07 '22 at 04:24
  • please don't edit the question to solve your issue. It's a typo, the q will be closed accordingly, or you can delete it yourself. – bolov Oct 07 '22 at 04:26
  • @bolov just realized about that, even with stof it rounds up. I chage it – Levbeux Oct 07 '22 at 04:27
  • Are you sure you're not just running out of available precision in the float variable? I think they're limited to 6 or 7 digits. Try `stod` instead of `stof`. – Mark Ransom Oct 07 '22 at 04:27
  • allow me to highly doubt that `stof` produces `101472` from input `101471.71`. Please double check, triple check your code and results. – bolov Oct 07 '22 at 04:28
  • @bolov why did you edit the question? It's clear that `stoi` was a typo, since it would return 101471 and not 101472. – Mark Ransom Oct 07 '22 at 04:30
  • oh, wow, I am wrong. `stof` does produce that result. My apologies. – bolov Oct 07 '22 at 04:31
  • ok, coming back with another update :)). I was not wrong. `stof` works. The issue must be on printing. Without any manips `cout` prints `101472`, but with `std::fixed` and `std::setpricision(2)` it prints `101472.71`. Although `setd` doesn't hurt, float has just enough precision for this number. – bolov Oct 07 '22 at 04:38
  • @bolov `std::stof` returns a float which has only ~6-7 digits of precision so it can't even be as precise as `101472.71`. You have to use `stod` for that – phuclv Oct 07 '22 at 05:06
  • Levbeux, "When I output that value with std::stof(str) it gets rounded." --> To output the value with no rounding, use [`std::hexfloat`](https://stackoverflow.com/a/19399113/2410359). – chux - Reinstate Monica Nov 02 '22 at 02:28

1 Answers1

1

The issue probably isn't with std::stof, but is probably with the default precision of 6 in std::cout. You can use std::setprecision to increase that precision and capture all of your digits.

Here's a program that demonstrates:

#include <iostream>
#include <iomanip>
#include <string>

int main() {
    std::cout << 101471.71f << "\n";
    std::cout << std::stof("101471.71") << "\n";
    std::cout << std::setprecision(8) << 101471.71f << "\n";
    std::cout << std::stof("101471.71") << "\n";
    return 0;
}

Outputs:

101472
101472
101471.71
101471.71

Be aware that std::setprecision sticks to the std::cout stream after it's called. Notice how the above example calls it exactly once but its effect sticks around.