Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Loss of '0's while converting from QString to float or QString to double
Forum Updated to NodeBB v4.3 + New Features

Loss of '0's while converting from QString to float or QString to double

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 4 Posters 790 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Swati777999S Offline
    Swati777999S Offline
    Swati777999
    wrote on last edited by
    #1

    I wrote the following codes for converting QString to double and QString to float

    EXAMPLE-1 : Conversion from QString to double

    QString myStr ="3.14000";
    double myStr_double = myStr.toDouble();
    qDebug() << "String to double conversion  =" <<myStr_double;  // Ans :3.14
    

    EXAMPLE-2 : Conversion from QString to float

    QString myStr ="3.14000";
    float myStr_float = myStr.toFloat();
    qDebug() << "String to float conversion  =" <<myStr_float;  // Ans :3.14
    

    In both the above cases, I'm getting 3.14 whereas I want to get the answer as 3.14000 without losing zeroes. Can someone suggest to me how to do it?

    “ In order to be irreplaceable, one must always be different” – Coco Chanel

    Pl45m4P 1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #4

      There's always a lot of confusions about string representations of floating point numbers.
      You have to think of the internal value of a float number as undeterminate until you decide to print it on the screen.
      And you have to decide the format you want to see ( number of digits, precision)

      asprintf is very handy for that:

         float f=3.14;
         qDebug()<<QString::asprintf("%10f",f);  // precision=6 by default
         qDebug()<<QString::asprintf("%10.4f",f);
         qDebug()<<QString::asprintf("%10.2f",f);
         qDebug()<<QString::asprintf("%010.2f",f);
      
      "  3.140000"
      "    3.1400"
      "      3.14"
      "0000003.14"
      
      1 Reply Last reply
      2
      • Swati777999S Swati777999

        I wrote the following codes for converting QString to double and QString to float

        EXAMPLE-1 : Conversion from QString to double

        QString myStr ="3.14000";
        double myStr_double = myStr.toDouble();
        qDebug() << "String to double conversion  =" <<myStr_double;  // Ans :3.14
        

        EXAMPLE-2 : Conversion from QString to float

        QString myStr ="3.14000";
        float myStr_float = myStr.toFloat();
        qDebug() << "String to float conversion  =" <<myStr_float;  // Ans :3.14
        

        In both the above cases, I'm getting 3.14 whereas I want to get the answer as 3.14000 without losing zeroes. Can someone suggest to me how to do it?

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #2

        @Swati777999

        You dont "lose" them... it's just the qDebug output which prints it like that.

        See:

        • https://doc.qt.io/qt-5/qdebug.html#formatting-options

        and further:

        • https://doc.qt.io/qt-5/qtextstream.html#qtextstream-manipulators
        • https://doc.qt.io/qt-5/qtextstream.html#setRealNumberNotation
        • https://doc.qt.io/qt-5/qtextstream.html#setRealNumberPrecision

        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        4
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by ChrisW67
          #3

          @Swati777999
          You seem to be confusing the human-readable representation of the number (i.e. your myStr) with the value of the number (i.e. myStr_double).

          3.14 is the numeric value. How that is converted into a human-readable representation is entirely up to you. Suppressing leading and trailing zeroes (because they generally carry no meaning) is the default behaviour of the vast majority of conversions from numeric value to readable-string ; in this case qDebug. Aside from @Pl45m4's suggestions you should also look at QString::number() and use a suitable precision (e.g. 5).

          1 Reply Last reply
          4
          • M Offline
            M Offline
            mpergand
            wrote on last edited by mpergand
            #4

            There's always a lot of confusions about string representations of floating point numbers.
            You have to think of the internal value of a float number as undeterminate until you decide to print it on the screen.
            And you have to decide the format you want to see ( number of digits, precision)

            asprintf is very handy for that:

               float f=3.14;
               qDebug()<<QString::asprintf("%10f",f);  // precision=6 by default
               qDebug()<<QString::asprintf("%10.4f",f);
               qDebug()<<QString::asprintf("%10.2f",f);
               qDebug()<<QString::asprintf("%010.2f",f);
            
            "  3.140000"
            "    3.1400"
            "      3.14"
            "0000003.14"
            
            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved