std::atof or std::stof (c++11) unable to convert properly ("1.24")
Asked Answered
D

0

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

Danedanegeld answered 30/5, 2016 at 13:18 Comment(5)
You must have the French comma style selected in your Windows options.Handbag
I'm on a Linux distro, actuallyDanedanegeld
You must have the French comma style selected in your Linux options.Handbag
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?Danedanegeld
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.Blagoveshchensk

© 2022 - 2024 — McMap. All rights reserved.