Results 1 to 5 of 5

Thread: qstring k="100.1",how to convert to int = 100 ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2009
    Posts
    28
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default qstring k="100.1",how to convert to int = 100 ?

    when i invoke toInt(), it return 0.
    now i first use atoi(), and then QString::number();
    but, it seems too much procedure???
    how can i do that only using QString's method ?
    thanks.

  2. #2
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: qstring k="100.1",how to convert to int = 100 ?

    Try
    Qt Code:
    1. QString::toDouble();
    To copy to clipboard, switch view to plain text mode 
    and then take the integer part!

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

    lanmanck (9th September 2009)

  4. #3
    Join Date
    Sep 2008
    Posts
    18
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qstring k="100.1",how to convert to int = 100 ?

    Alternatively, you can strip the decimals away from the string:
    Qt Code:
    1. QString str("100.1");
    2. int number = str.left(str.indexOf('.')).toInt(); // == 100
    To copy to clipboard, switch view to plain text mode 
    Works even if there isn't a decimal part, because str.left(-1) returns the whole string.
    Last edited by hkvm; 8th September 2009 at 16:34.

  5. The following user says thank you to hkvm for this useful post:

    lanmanck (9th September 2009)

  6. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qstring k="100.1",how to convert to int = 100 ?

    for me this should be enough:
    Qt Code:
    1. QString str("100.1");
    2. int x = str.toDouble();
    To copy to clipboard, switch view to plain text mode 
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  7. The following user says thank you to faldzip for this useful post:

    lanmanck (9th September 2009)

  8. #5
    Join Date
    Aug 2009
    Posts
    28
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: qstring k="100.1",how to convert to int = 100 ?

    i appreciate all the replyies

Similar Threads

  1. convert from QString to char[sizeof(...)]
    By adamatic in forum Qt Programming
    Replies: 4
    Last Post: 3rd September 2011, 09:05
  2. convert QString to QByteArray
    By morgana in forum Qt Programming
    Replies: 5
    Last Post: 2nd March 2011, 13:33
  3. How to convert QString to std::string or char*?
    By yangyunzhao in forum Qt Programming
    Replies: 26
    Last Post: 26th August 2009, 06:52
  4. File rename detection
    By bunjee in forum Qt Programming
    Replies: 6
    Last Post: 23rd July 2009, 15:22
  5. convert QString to int
    By mattia in forum Newbie
    Replies: 2
    Last Post: 4th January 2008, 09:10

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.