Results 1 to 2 of 2

Thread: All digits of float to QString

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

    Question All digits of float to QString

    Hi,

    I need to convert float to QString so that all original digits (including 0's) are preserved. In the example below 100.0 becomes 100 and I don't want that. How would I do the conversion so that all digits are preserved?

    The example shows where my floating number comes from and where it needs to go. I know it seems like running in a circle but my script requires this.
    Qt Code:
    1. char TestChar[]="100.0";
    2. QString MyChar = TestChar;
    3. float TestFloat = MyChar.toFloat();
    4.  
    5. QString float_to_text;
    6. float_to_text.setNum(TestFloat);
    To copy to clipboard, switch view to plain text mode 

    Alternatively I'd like to know what is the best way to tell if an entry from a file is int or float? Does Qt offer such function? I'd like it to recognize "100.0" as a float.

    Thanks!

  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: All digits of float to QString

    You can format a number as a string with a specified number of decimal places using QString::number() and the 'f' format (or its QLocale equivalent). If you need to present exactly what the user entered back to the user then keep it as a string. Once you convert it to float you have no way to distinguish between "100", "100.0" and "100.00000": 100 is 100 (and integer) no matter how many zeros you append after the decimal point.

    Recognising a string as a floating point or integer number is not trivial unless you restrict yourself to one location. For example, "1,100.0" is a number in some locales and not others where it may be expressed as "1.000,0" or "1000.0". You could use a simple regular expression to tell "100.0" apart from "100" but you have to know exactly what your input might look like. What would you do with 1100 expressed as "1.1e3" or "100." for example?

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

    timmu (4th January 2012)

Similar Threads

  1. converting QString to FLOAT behaves unexpectedly
    By timmu in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2011, 10:46
  2. Replies: 4
    Last Post: 18th May 2011, 17:59
  3. Replies: 1
    Last Post: 10th February 2009, 09:42
  4. setNum() digits question
    By tommy in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2008, 15:19
  5. coordinal significant digits
    By nitriles in forum Qt Programming
    Replies: 1
    Last Post: 27th October 2007, 15:54

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.