6

I'm a little bit frustrated after I've found this strange atof/stof behavior

double bg = std::stof("1,24");
std::cerr<<"comma: "<<bg<<std::endl;
bg = std::stof("1.24");
std::cerr<<"dot: "<<bg<<std::endl;

when I change the string format, from comma to dot, this is what happens:

comma: 1.24
dot: 1

has anyone encountered this problem? thanks

bart

bartux
  • 111
  • 5
  • 5
    You must have the French comma style selected in your Windows options. – QuentinUK May 30 '16 at 13:21
  • 2
    I'm on a Linux distro, actually – bartux May 30 '16 at 13:23
  • 11
    You must have the French comma style selected in your Linux options. – QuentinUK May 30 '16 at 13:24
  • I've solved using istringstream, but however the problem is not the "french comma style" That one, the "1.24", was just an example. I've encountered the problem reading data from a file, generated with fstream @QuentinUK, thanks for the answers. However, how can I disable this option? – bartux May 30 '16 at 13:36
  • You could call `imbue` on the stream object and set a locale without that problem. You could also use `std::locale::global(std::locale("en_US.UTF-8"));` on your main function but this has a problem that any other thread of your app could change it. – codestation May 30 '16 at 13:59

0 Answers0