0

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)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    You might have a different locale setting between debug and release. – doug Sep 09 '20 at 02:23
  • 4
    Perhaps "running mode" is using a different locale to "debug mode". In US numeric locales, `99,03` are two integers separated by a comma. In some European locales, `99,03` is a decimal value 99.03 – paddy Sep 09 '20 at 02:23

1 Answers1

2

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.

Louis Go
  • 2,213
  • 2
  • 16
  • 29