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. How to get rid of scientific notation while converting from QString to float
Forum Updated to NodeBB v4.3 + New Features

How to get rid of scientific notation while converting from QString to float

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 6 Posters 2.7k Views 3 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

    Here's my code

        QString myStr ="03145678";
        qDebug() <<"String input ="<<myStr;   // String input = "03145678"
        float myFloat = myStr.toFloat();
        qDebug()<< "String to float conversion = " << myFloat;   //= 3.14568e+06
    

    I don't want the result as 3.14568e+06 , I want to display it as 03145678 without losing any digit.

    When I tried it with .toInt() , I got the result as 3145678 by losing the leading zero.

    How can I retain all the digits of the String while converting it to other data types like float,double or int ?

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

    Pl45m4P S 2 Replies Last reply
    0
    • Swati777999S Swati777999

      Here's my code

          QString myStr ="03145678";
          qDebug() <<"String input ="<<myStr;   // String input = "03145678"
          float myFloat = myStr.toFloat();
          qDebug()<< "String to float conversion = " << myFloat;   //= 3.14568e+06
      

      I don't want the result as 3.14568e+06 , I want to display it as 03145678 without losing any digit.

      When I tried it with .toInt() , I got the result as 3145678 by losing the leading zero.

      How can I retain all the digits of the String while converting it to other data types like float,double or int ?

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

      @Swati777999

      Same answer as here: Change the qDebug notation.
      Haven't tried but there's something like Qt::fixed and Qt::scientific which you can use in your print command.

      • https://doc.qt.io/qt-5/qtextstream.html#RealNumberNotation-enum

      Edit:

      Similar topic here:

      • https://forum.qt.io/topic/75586/showing-numbers-in-decimal-not-scientific-notation

      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
      2
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by JKSH
        #3

        qDebug() is for quickly checking your data.

        If you want to format and display your data, convert it to a QString with the exact formatting that you want. Then, display it in a QLabel or another display widget.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        Swati777999S 1 Reply Last reply
        4
        • JKSHJ JKSH

          qDebug() is for quickly checking your data.

          If you want to format and display your data, convert it to a QString with the exact formatting that you want. Then, display it in a QLabel or another display widget.

          Swati777999S Offline
          Swati777999S Offline
          Swati777999
          wrote on last edited by
          #4

          @JKSH said in How to get rid of scientific notation while converting from QString to float:

          qDebug() is for quickly checking your data.

          If you want to format and display your data, convert it to a QString with the exact formatting that you want. Then, display it in a QLabel or another display widget.

          I'm running this code in Qt console.

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

          JKSHJ 1 Reply Last reply
          0
          • Swati777999S Swati777999

            @JKSH said in How to get rid of scientific notation while converting from QString to float:

            qDebug() is for quickly checking your data.

            If you want to format and display your data, convert it to a QString with the exact formatting that you want. Then, display it in a QLabel or another display widget.

            I'm running this code in Qt console.

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            @Swati777999 said in How to get rid of scientific notation while converting from QString to float:

            I'm running this code in Qt console.

            OK, then use QTextStream to print to stdout.

            You cal call the following functions to format your numbers:

            • QTextStream::setNumberFlags()
            • QTextStream::setRealNumberNotation()
            • QTextStream::setFieldWidth()
            • QTextStream::setPadChar()

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            Swati777999S 1 Reply Last reply
            4
            • JKSHJ JKSH

              @Swati777999 said in How to get rid of scientific notation while converting from QString to float:

              I'm running this code in Qt console.

              OK, then use QTextStream to print to stdout.

              You cal call the following functions to format your numbers:

              • QTextStream::setNumberFlags()
              • QTextStream::setRealNumberNotation()
              • QTextStream::setFieldWidth()
              • QTextStream::setPadChar()
              Swati777999S Offline
              Swati777999S Offline
              Swati777999
              wrote on last edited by
              #6

              @JKSH said in How to get rid of scientific notation while converting from QString to float:

              @Swati777999 said in How to get rid of scientific notation while converting from QString to float:

              I'm running this code in Qt console.

              OK, then use QTextStream to print to stdout.

              You cal call the following functions to format your numbers:

              • QTextStream::setNumberFlags()
              • QTextStream::setRealNumberNotation()
              • QTextStream::setFieldWidth()
              • QTextStream::setPadChar()

              It's so frustrating not to see the code working correctly when you follow Qt Docs.

                  QString myStr ="03145678";
                  qDebug() <<"String input ="<<myStr;
                  qDebug()<<"After applying QTextStream = <<" << QTextStream::setRealNumberNotation(myStr::2); //Syntax Error
              

              I am unable to understand the synatx from the Qt Doc. :( and it's so frustrating for me!

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

              jsulmJ KroMignonK 2 Replies Last reply
              0
              • Swati777999S Swati777999

                @JKSH said in How to get rid of scientific notation while converting from QString to float:

                @Swati777999 said in How to get rid of scientific notation while converting from QString to float:

                I'm running this code in Qt console.

                OK, then use QTextStream to print to stdout.

                You cal call the following functions to format your numbers:

                • QTextStream::setNumberFlags()
                • QTextStream::setRealNumberNotation()
                • QTextStream::setFieldWidth()
                • QTextStream::setPadChar()

                It's so frustrating not to see the code working correctly when you follow Qt Docs.

                    QString myStr ="03145678";
                    qDebug() <<"String input ="<<myStr;
                    qDebug()<<"After applying QTextStream = <<" << QTextStream::setRealNumberNotation(myStr::2); //Syntax Error
                

                I am unable to understand the synatx from the Qt Doc. :( and it's so frustrating for me!

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Swati777999 said in How to get rid of scientific notation while converting from QString to float:

                setRealNumberNotation

                You know that this is NOT a static method?
                Also, why do you mix qDebug and QTextStream? This is NOT what @JKSH suggested.

                QTextStream out(stdout);
                out.setRealNumberNotation...
                out << ...
                

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • Swati777999S Swati777999

                  @JKSH said in How to get rid of scientific notation while converting from QString to float:

                  @Swati777999 said in How to get rid of scientific notation while converting from QString to float:

                  I'm running this code in Qt console.

                  OK, then use QTextStream to print to stdout.

                  You cal call the following functions to format your numbers:

                  • QTextStream::setNumberFlags()
                  • QTextStream::setRealNumberNotation()
                  • QTextStream::setFieldWidth()
                  • QTextStream::setPadChar()

                  It's so frustrating not to see the code working correctly when you follow Qt Docs.

                      QString myStr ="03145678";
                      qDebug() <<"String input ="<<myStr;
                      qDebug()<<"After applying QTextStream = <<" << QTextStream::setRealNumberNotation(myStr::2); //Syntax Error
                  

                  I am unable to understand the synatx from the Qt Doc. :( and it's so frustrating for me!

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by KroMignon
                  #8

                  @Swati777999 said in How to get rid of scientific notation while converting from QString to float:

                  It's so frustrating not to see the code working correctly when you follow Qt Docs.

                  QString myStr ="03145678";
                  qDebug() <<"String input ="<<myStr;
                  qDebug()<<"After applying QTextStream = <<" << QTextStream::setRealNumberNotation(myStr::2); //Syntax Error
                  

                  I am unable to understand the synatx from the Qt Doc. :( and it's so frustrating for me!

                  I don't want to hurt you, but this is standard C++ syntax. As I already told you C++ and Python are 2 different languages in almost all aspects.
                  So if you want to develop in C++ you have to learn C++ basics, as you have done for python.

                  C++ is not better or worst than python, it is just completely different.

                  The C++ code you have written don't make sense, so it cannot work:

                  • myStr::2 is non sense, this would mean you want to get a static member called 2 (which is not allowed a member name!) from class used by myStr, which is QString
                  • QTextStream::setRealNumberNotation() means in C++ that you want to call static method setRealNumberNotation of calss QTextStream which do not exists!

                  So please, take time to learn C++ if you want to program in C++.

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  1 Reply Last reply
                  2
                  • Swati777999S Swati777999

                    Here's my code

                        QString myStr ="03145678";
                        qDebug() <<"String input ="<<myStr;   // String input = "03145678"
                        float myFloat = myStr.toFloat();
                        qDebug()<< "String to float conversion = " << myFloat;   //= 3.14568e+06
                    

                    I don't want the result as 3.14568e+06 , I want to display it as 03145678 without losing any digit.

                    When I tried it with .toInt() , I got the result as 3145678 by losing the leading zero.

                    How can I retain all the digits of the String while converting it to other data types like float,double or int ?

                    S Offline
                    S Offline
                    SimonSchroeder
                    wrote on last edited by
                    #9

                    @Swati777999 said in How to get rid of scientific notation while converting from QString to float:

                    When I tried it with .toInt() , I got the result as 3145678 by losing the leading zero.

                    There is no built-in number type that would store leading zeros. If this is what you want you need to look for another solution. You can specify the width of the output with a padding of '0's to the left. What kind of numbers do you want to handle? What do they represent? Why should they have a leading zero?

                    1 Reply Last reply
                    1

                    • Login

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