Results 1 to 2 of 2

Thread: converting QString to FLOAT behaves unexpectedly

  1. #1
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Question converting QString to FLOAT behaves unexpectedly

    Hi,

    When I do this:

    Qt Code:
    1. QString forty= "40.1";
    2. float frty = forty.toFloat();
    3. printf("%f\n", frty);
    To copy to clipboard, switch view to plain text mode 

    I get:
    40.099998

    Why don't I get 40.1? I'm very thankful for any ideas.
    Thanks!

  2. #2
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Thanks
    10
    Thanked 27 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: converting QString to FLOAT behaves unexpectedly

    There's nothing wrong with QString::toFloat(), it's result of casting your value to double inside printf. Some more info here.
    Try this
    Qt Code:
    1. qDebug() << frty;
    To copy to clipboard, switch view to plain text mode 
    Oleg Shparber

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

    timmu (15th December 2011)

Similar Threads

  1. Replies: 4
    Last Post: 18th May 2011, 17:59
  2. converting uint to QString
    By Yayati.Ekbote in forum Newbie
    Replies: 1
    Last Post: 14th April 2010, 13:43
  3. Converting a QString to a LPCTSTR?
    By dobedidoo in forum Qt Programming
    Replies: 3
    Last Post: 7th December 2009, 14:27
  4. Converting u_char to QString
    By merlvingian in forum Newbie
    Replies: 7
    Last Post: 29th September 2006, 00:11
  5. QString to char* not converting
    By DPinLV in forum Qt Programming
    Replies: 17
    Last Post: 6th August 2006, 12:15

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.