Results 1 to 7 of 7

Thread: How to convert QString to float and compare values?

  1. #1
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default How to convert QString to float and compare values?

    I would like to convert QString values to float and then compare them..

    I have used toFloat(), toDouble() , but it is not working.

    Qt Code:
    1. QString version1 = "2.02.01";
    2. QString version 2 = "2.02.02"
    3. if(version1.toFloat() < version2.toFloat()) // just 2 is returned everytime.
    4. {
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 

    How to resolve it???

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to convert QString to float and compare values?

    A floating point number can have only one decimal point. These have two thus they are not valid floating point numbers.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to convert QString to float and compare values?

    Could you explain how to compare those two values?
    QString version1 = "2.02.01";
    QString version 2 = "2.02.02"

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to convert QString to float and compare values?

    There are couple of ways

    1. Compare the string directly, character by character
    or
    2. Split into substrings, then convert eash substring to integer, then compare the integers.
    or
    3. Write a special function to convert into comparable numbers (weights)
    or
    4. Just remove the delimiters, then convert to integers, then compare
    Example
    Qt Code:
    1. QString version1 = "2.02.01";
    2. QString version2 = "2.02.02";
    3.  
    4. while(version1.contains("."))
    5. version1 = version1.remove(QString("."));
    6.  
    7. while(version2.contains("."))
    8. version2 = version2.remove(QString("."));
    9.  
    10. if(version1.toUInt() < version2.toUInt())
    11. {
    12. ...
    13. }
    To copy to clipboard, switch view to plain text mode 
    or
    5. Maintain a map table to compare
    Example
    Qt Code:
    1. QString version1 = "2.02.01";
    2. QString version2 = "2.02.02";
    3.  
    4. QMap<int, QString> map;
    5. map.insert(1, "2.02.01");
    6. map.insert(2, "2.02.02");
    7. if(map.key(version1) < map.key(version2))
    8. {
    9. ....
    10. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to convert QString to float and compare values?

    If the strings are always compared to that format you can use this function :
    Qt Code:
    1. int ver2int( QString const &ver )
    2. {
    3. QStringList m_list = ver.split('.');
    4. int ret_val = 0;
    5.  
    6. for( int i = 0; i < m_list.size(); i++ )
    7. ret_val = 100*ret_val + m_list.at(i).toInt();
    8.  
    9. return ver2int;
    10. }
    11.  
    12. QString version1 = "2.02.01";
    13. QString version2 = "2.02.02";
    14.  
    15. if(ver2int(version1) < ver2int(version2))
    16. {
    17. ....
    18. }
    To copy to clipboard, switch view to plain text mode 

    Not tested, it should work.

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to convert QString to float and compare values?

    Lesiok's example is what I ment in my 3rd bullet, there you go one more example
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. #7
    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: How to convert QString to float and compare values?

    If they are always in exactly that format, i.e. "n.nn.nn" where n is a digit, then a straight string comparison for less-than, greater-than or equal-to comparison should work.

Similar Threads

  1. Replies: 3
    Last Post: 12th July 2012, 07:41
  2. How to convert string hex values to its real binary values?
    By AttilaPethe in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2012, 22:47
  3. How to convert int to float?
    By babygal in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2010, 05:06
  4. QDoubleSpinBox and Float Values
    By George Neil in forum Qt Programming
    Replies: 9
    Last Post: 5th August 2008, 20:22
  5. Replies: 4
    Last Post: 31st January 2008, 20:44

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.