PDA

View Full Version : converting QString to FLOAT behaves unexpectedly



timmu
15th December 2011, 10:34
Hi,

When I do this:



QString forty= "40.1";
float frty = forty.toFloat();
printf("%f\n", frty);


I get:
40.099998

Why don't I get 40.1? I'm very thankful for any ideas.
Thanks!

Oleg
15th December 2011, 10:46
There's nothing wrong with QString::toFloat(), it's result of casting your value to double inside printf. Some more info here (http://stackoverflow.com/questions/6532502/why-does-a-c-floating-point-type-modify-the-actual-input-of-125-1-to-125-099998).
Try this :)


qDebug() << frty;