Results 1 to 3 of 3

Thread: QString.toFloat() precision.

  1. #1
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QString.toFloat() precision.

    Hello people,

    Let's say i have a QString named szNum containing "4532.361670".
    i want to put it in a float, so i'm doing
    Qt Code:
    1. float fNum = szNum.toFloat();
    To copy to clipboard, switch view to plain text mode 
    Then when i do a
    Qt Code:
    1. qDebug () << fNum
    To copy to clipboard, switch view to plain text mode 
    the output is "4532.36".
    So, from here i have two guesses:
    - My number is fine, but qDebug doesn't display it with the precision after 2.
    - My number didnt get converted from QString to float with a good precision.

    So the question is: is there a QT function to convert such a string to a float with a better precision? Or do i have to split my string on the dot ".", then store the int part in my float, then add to my float the decimal part divided by 1^(Numbers_Of_Digits+1)?

    Thanks ^^
    Pierre.
    Last edited by hickscorp; 7th December 2007 at 00:57. Reason: spelling error

  2. #2
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QString.toFloat() precision.

    The float data type usually consists of 8 bytes of data, and can represent 6 significant digits. If you do the same experiment with, say, 0.4532361670, then I believe the output would be 0.453236. So, don't worry about the number of digits after the decimal point. It's a floating point number, so the decimal point can float. If you need more digits, use double.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QString.toFloat() precision.

    Qt Code:
    1. QString str("4532.361670");
    2. qDebug() << qSetRealNumberPrecision(10) << str.toDouble();
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

Similar Threads

  1. QThread msleep precision
    By Lele in forum Qt Programming
    Replies: 3
    Last Post: 8th November 2007, 23:40
  2. very simple precision question
    By vonCZ in forum General Programming
    Replies: 6
    Last Post: 28th June 2007, 11:46
  3. QPainter clipping with precision behind the decimal point
    By Pieter from Belgium in forum Qt Programming
    Replies: 0
    Last Post: 14th March 2007, 13:30
  4. precision problem - Help pls
    By munna in forum General Programming
    Replies: 7
    Last Post: 26th May 2006, 04:00
  5. Current Time with microsecond precision on windows
    By Dwarf007 in forum General Programming
    Replies: 16
    Last Post: 5th April 2006, 11:42

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.