Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

toDouble() is returning wrong numerical value



  • I'm storing the value entered by a user inside a lineEdit into a double which I declared in my code... the thing is that the value stored in this double is not always the same one as entered
    I entered the number 28.05 and the value stored inside the double is 28.050000000000001 which causes problems

    my code:

    num = ui->lineEdit->text().toDouble();
    

  • Moderators

    This is just how floating point numbers work. It's the same in C++ and other languages, it's one of the fundamental things about how computers store data.

    If you want to store exactly what user wrote - store the string. When you need to do maths with the value, convert it to double only for the purpose of the algorithm.



  • @Sampi
    Exactly as @sierdzio says about how floating point numbers are stored. They are always only approximate values close to your desired value.

    You could store as strings, as he says, and then convert/to from numbers when needed. Or, some languages (Python. SQL) have a "decimal" type, which have a set number of places after the decimal point so the representation is perfectly accurate to that limit. C++ does not come with that; there are libraries which will provide such with corresponding arithmetic. Or you could roll your own struct which has ints for the numbers before & after the decimal place separately, combining them when necessary. Another trick: if, say, all your numbers are money and so have 2 decimal places, then do all your math by multiplying them by up by 100 so that they become ints and then arithmetic operations won't lose precision: only convert numbers from/to the decimal place representation during input/output.


Log in to reply