I want to read ".text" file and convert string as double. In debug mode i can read text as 99,03 but in running mode i can read only 99. I can't understand how it would be? My converting code is below
double a = std::stod(text)
I want to read ".text" file and convert string as double. In debug mode i can read text as 99,03 but in running mode i can read only 99. I can't understand how it would be? My converting code is below
double a = std::stod(text)
std::stod
is affected by locale.
#include <iostream>
#include <string>
#include <clocale>
int main(void){
std::setlocale(LC_ALL, "C");
std::string s{"99.03"};
double d = std::stod( s );
std::cout<< d << "\n";
std::setlocale(LC_ALL, "de_DE.UTF-8"); // A locale installed on your machine.
std::string s2{"99,03"};
double d2 = std::stod( s2 );
std::cout<< d2 << "\n";
}
You may get the result as following: ( tested on msvc and gcc 9.3.0 )
99.03
99.03
If you want std::cout
also print comma as decimal separator, imbue
is required. You may refer to this post.