Results 1 to 3 of 3

Thread: String ( QbyteArray ) to Double Value with 6 decimal digits

  1. #1
    Join Date
    Jul 2015
    Posts
    3
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default String ( QbyteArray ) to Double Value with 6 decimal digits

    Hi ,

    I have to convert a string to a double value , my string is inside a QByteArray :

    Qt Code:
    1. QByteArray myStr = "50.123456";
    2. double myDouble = myStr.toDouble();
    To copy to clipboard, switch view to plain text mode 

    it will return 50.12346 , I need exact value , how to fix this problem ?

    Best Regards,
    Amir .

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: String ( QbyteArray ) to Double Value with 6 decimal digits

    This could be the exact value, but our output doesn't have enough precision, or it is the closest value a double can hold.

    Cheers,
    _

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: String ( QbyteArray ) to Double Value with 6 decimal digits

    The closest approximation of 50.123456 in a double is 5.01234559999999973456397128757E1 (5.0123455047607421875E1 in a float). If you must have the exact value then you should not be using double but some precise storage mechanism, an integer number of millionths for example, and do any manipulations in that form.

    If all you need is to be able to reproduce the string representation to six decimal places then either float or double approximation may be acceptable but you need to format the number correctly with QString::number() or the like.

Similar Threads

  1. Replies: 1
    Last Post: 21st December 2012, 12:00
  2. Replies: 7
    Last Post: 28th December 2010, 21:27
  3. Searching for a string in QByteArray
    By nagabathula in forum Qt Programming
    Replies: 16
    Last Post: 24th November 2010, 12:39
  4. hex QByteArray to double?
    By tlt in forum Qt Programming
    Replies: 1
    Last Post: 12th July 2009, 04:33
  5. Convert from QByteArray to String ?
    By probine in forum Newbie
    Replies: 3
    Last Post: 25th March 2006, 16:49

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.