Results 1 to 11 of 11

Thread: toDouble() and rounding problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    Join Date
    Feb 2008
    Posts
    50
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: toDouble() and rounding problem

    I`m using double everywhere. Lemme try your code and i`ll be back.

    Edit:

    Qt Code:
    1. str << "100000.55" << "10000.55" << "1000.55";
    2. foreach(const QString &s, str){
    3. printf("%f\n", s.toDouble());
    4. qDebug() << "qDebug: " << s.toDouble();
    5. }
    To copy to clipboard, switch view to plain text mode 

    This code gives me the next results:

    100000.550000
    qDebug: 100001

    10000.550000
    qDebug: 10000.5

    1000.550000
    qDebug: 1000.55

    Why is that rounding and any idea [in code] how to get the real precise value from the database without the rounding?

    Edit2: Or should i have to use sprintf(), atof() or something like that so i can get the precise value into a double? That`s too... complicated.

    Edit3: wysota, try to put a bigger number with 2 digits after the floating point like 100000.55 and in the second example u gave me it will round the bigger number. It will make it 100001.. thats 0.45 cents gain per calculation. Not good no precise

    Edit4:

    Qt Code:
    1.  
    2. while (query.next()) {
    3. eur = query.value(1).toByteArray();
    4. }
    5. //eur == 100000.55
    6. char *euC = eu.data();
    7. double euDbl = atof(euC);
    8. qDebug() << euC << euDbl;
    9. //result is: 100000.55 100001
    To copy to clipboard, switch view to plain text mode 

    What is wrong here... 5 digits numbers get rounded 1 digit after the point, 6 digits numbers get rounded to an integer. OR nothing is wrong and i`m in the biggest math mistake in my life lol

    100000.55 goes 100001
    10000.55 goes 10000.5
    1000.55 goes 1000.55
    That rounding happens when converting from database result to number (double)
    Is it normal? Or using floats or doubles with currencies is a no no.
    Last edited by sadjoker; 27th August 2008 at 16:56.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.