Results 1 to 8 of 8

Thread: Conversion from string to double and precision

  1. #1
    Join Date
    Mar 2010
    Location
    Europe
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Conversion from string to double and precision

    Hi, I need to read a string from an xml file and then convert it to double. My problem is that I can't find a way to specify precision, so I lose some decimals. Example:
    Qt Code:
    1. qDebug() << c.toDouble("44.48666300");
    To copy to clipboard, switch view to plain text mode 
    this prints 44.4867. I need to set the locale since the file uses always the dot as a decimal separator.
    It seems that I can specify precision only if I convert a double into a string, not the opposite; do you have any advice? I guess I'll have to use directly some C++ standard function
    Thanks in advance

  2. #2
    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: Conversion from string to double and precision

    Your qDebug() line is converting a double to a string in a default format (See the double version of QLocale::toString() and the source to the QDebug << operator):
    Qt Code:
    1. double d = c.toDouble("44.48666300");
    2. qDebug() << d;
    3. qDebug() << QString::number(d);
    4. qDebug() << c.toString(d, 'f', 10);
    To copy to clipboard, switch view to plain text mode 
    outputs:
    Qt Code:
    1. 44.4867
    2. "44.4867"
    3. "44.4866630000"
    To copy to clipboard, switch view to plain text mode 
    All the possible bits of the floating point representation of your original string are in the double. Note that this may still be an approximation due to the nature of floats.
    Last edited by ChrisW67; 25th March 2010 at 22:40. Reason: Add where to find the defaults

  3. The following user says thank you to ChrisW67 for this useful post:

    Klenje (25th March 2010)

  4. #3
    Join Date
    Mar 2010
    Location
    Europe
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Conversion from string to double and precision

    Thanks a lot, I should have thought about qDebug automatic conversion

  5. #4
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Conversion from string to double and precision

    i have an issue with the precision of the conversion.

    Qt Code:
    1. QString str="0.1";
    2. double x = str.toDouble();//i get 0.1000000001 !!
    3. x= 2*x;//i get 0.200000003
    To copy to clipboard, switch view to plain text mode 

    is there a way to correct this?!

  6. #5
    Join Date
    Nov 2010
    Posts
    97
    Thanks
    6
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Conversion from string to double and precision

    Quote Originally Posted by fatecasino View Post
    i have an issue with the precision of the conversion.

    Qt Code:
    1. QString str="0.1";
    2. double x = str.toDouble();//i get 0.1000000001 !!
    3. x= 2*x;//i get 0.200000003
    To copy to clipboard, switch view to plain text mode 

    is there a way to correct this?!
    No, not with the standard floating point types anyway. This is an architectural problem caused by base2 floating point numbers. There's simply put, no way to fix it. How you respond to it depends on your needs.

  7. #6
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Conversion from string to double and precision

    x values in my problem will not be much different that 0.1, or 0.01 (it is a STEP value for a plot function).
    Is there a special Qt function to chop off the last 5-6 digits?

  8. #7
    Join Date
    Nov 2010
    Posts
    97
    Thanks
    6
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Conversion from string to double and precision

    Quote Originally Posted by fatecasino View Post
    x values in my problem will not be much different that 0.1, or 0.01 (it is a STEP value for a plot function).
    Is there a special Qt function to chop off the last 5-6 digits?
    Probably, but why tie yourself to one UI? This kind of thing is more a job for something like boost.format or printf (though I'd go with a Qt version rather than printf).

  9. #8
    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: Conversion from string to double and precision

    i have an issue with the precision of the conversion.
    There is no way to perfectly represent 0.1 in binary floating point (without infinite bits). This conversion is as accurate as it can be within the size of the floating point number type (double or float).
    x values in my problem will not be much different that 0.1, or 0.01 (it is a STEP value for a plot function).
    Is there a special Qt function to chop off the last 5-6 digits?
    For display purposes you can format the number, rounded at a certain number digits to the right of the of decimal point, using QString::number() using the 'e', 'E', or 'F' formats (or the QLocale equivalent).

Similar Threads

  1. int to String Conversion
    By aj2903 in forum Qt Programming
    Replies: 4
    Last Post: 4th December 2009, 22:43
  2. The precision range of double
    By nikhilqt in forum Qt Programming
    Replies: 14
    Last Post: 8th July 2009, 15:08
  3. Replies: 1
    Last Post: 24th July 2008, 18:20
  4. Conversion Char Array to string
    By anafor2004 in forum Newbie
    Replies: 6
    Last Post: 6th May 2008, 14:35
  5. double precision or what?
    By mickey in forum General Programming
    Replies: 7
    Last Post: 20th February 2007, 20:01

Tags for this Thread

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.