Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. toDouble() is returning wrong numerical value
Forum Updated to NodeBB v4.3 + New Features

toDouble() is returning wrong numerical value

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 364 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sampi
    wrote on last edited by
    #1

    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();
    
    JonBJ 1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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.

      (Z(:^

      1 Reply Last reply
      7
      • S Sampi

        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();
        
        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #3

        @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.

        1 Reply Last reply
        2

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved