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. Type Conversion : from QString to float and double

Type Conversion : from QString to float and double

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 8 Posters 9.1k Views
  • 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.
  • J JKSH
    7 Mar 2022, 07:31

    @Swati777999 said in Type Conversion : from QString to float and double:

    So how can I store the exact number?

    Questions:

    1. Did you try the link I gave you?
    2. Why do you need this value to 9 significant digits?
    S Offline
    S Offline
    Swati777999
    wrote on 7 Mar 2022, 07:58 last edited by
    #6

    @JKSH said in Type Conversion : from QString to float and double:

    @Swati777999 said in Type Conversion : from QString to float and double:

    So how can I store the exact number?

    Questions:
    1. Did you try the link I gave you?

    Yes but there's no scope for conversion from string format [given in the link].

    2. Why do you need this value to 9 significant digits?

    I want the answer to be shown in a non-string format. So, I want all digits of string to be shown without losing any digit during display.

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

    J 1 Reply Last reply 7 Mar 2022, 08:47
    0
    • S Swati777999
      7 Mar 2022, 06:48

      I have compiled the following code:

      QString myValue = "313.567889";
      qDebug()<<" value after converting to float=" << myValue.toFloat();  // Output - 313.568
      qDebug()<<" value after converting to double=" << myValue.Double();  // Output - 313.568
      

      As you can see in both of the conversion , I got 313.568 whereas I wanted to get 313.567889. How can this conversion be done?

      K Offline
      K Offline
      KroMignon
      wrote on 7 Mar 2022, 08:17 last edited by KroMignon 3 Jul 2022, 08:18
      #7

      @Swati777999 said in Type Conversion : from QString to float and double:

      As you can see in both of the conversion , I got 313.568 whereas I wanted to get 313.567889. How can this conversion be done?

      Please take in consideration the answer you have already got.

      real values converted in float or double are converted in a specific format (called IEEE 754). So this is an approching value not the exact value. It is not possible with float or double to store a precise value!

      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
      0
      • S Swati777999
        7 Mar 2022, 07:58

        @JKSH said in Type Conversion : from QString to float and double:

        @Swati777999 said in Type Conversion : from QString to float and double:

        So how can I store the exact number?

        Questions:
        1. Did you try the link I gave you?

        Yes but there's no scope for conversion from string format [given in the link].

        2. Why do you need this value to 9 significant digits?

        I want the answer to be shown in a non-string format. So, I want all digits of string to be shown without losing any digit during display.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 7 Mar 2022, 08:47 last edited by
        #8

        @Swati777999 said in Type Conversion : from QString to float and double:

        I want the answer to be shown in a non-string format.

        If you already have the number as string why do you need to convert it to float/double and then back to show it? Why don't you simply show your string?

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

        J S 2 Replies Last reply 7 Mar 2022, 08:51
        1
        • J jsulm
          7 Mar 2022, 08:47

          @Swati777999 said in Type Conversion : from QString to float and double:

          I want the answer to be shown in a non-string format.

          If you already have the number as string why do you need to convert it to float/double and then back to show it? Why don't you simply show your string?

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 7 Mar 2022, 08:51 last edited by
          #9

          @jsulm Maybe, just maybe I have no proof for it :D, the op has a high precision string and wants it rounded and not simply truncated to a certain number of digits.

          would explain a couple of things


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • J jsulm
            7 Mar 2022, 08:47

            @Swati777999 said in Type Conversion : from QString to float and double:

            I want the answer to be shown in a non-string format.

            If you already have the number as string why do you need to convert it to float/double and then back to show it? Why don't you simply show your string?

            S Offline
            S Offline
            Swati777999
            wrote on 7 Mar 2022, 08:51 last edited by
            #10

            @jsulm
            It is required to be shown in a non-string format.

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

            J J 2 Replies Last reply 7 Mar 2022, 08:51
            0
            • S Swati777999
              7 Mar 2022, 08:51

              @jsulm
              It is required to be shown in a non-string format.

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 7 Mar 2022, 08:51 last edited by J.Hilk 3 Jul 2022, 08:55
              #11

              @Swati777999 said in Type Conversion : from QString to float and double:

              @jsulm
              It is required to be shown in a non-string format.

              what does that mean ? shown where ?


              in anticipating, does this mean "non string format" for you ?

              QString myValue = "313.567889";
              QDebug debug = qDebug();
              debug.noquote();
              debug << myValue;
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0
              • S Swati777999
                7 Mar 2022, 08:51

                @jsulm
                It is required to be shown in a non-string format.

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 7 Mar 2022, 09:09 last edited by
                #12

                @Swati777999 said in Type Conversion : from QString to float and double:

                It is required to be shown in a non-string format

                I don't get it. Everything you see on the screen is shown as string. What do you think qDebug is doing with your number to show it?

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

                1 Reply Last reply
                1
                • S Swati777999
                  7 Mar 2022, 06:48

                  I have compiled the following code:

                  QString myValue = "313.567889";
                  qDebug()<<" value after converting to float=" << myValue.toFloat();  // Output - 313.568
                  qDebug()<<" value after converting to double=" << myValue.Double();  // Output - 313.568
                  

                  As you can see in both of the conversion , I got 313.568 whereas I wanted to get 313.567889. How can this conversion be done?

                  M Offline
                  M Offline
                  mpergand
                  wrote on 7 Mar 2022, 15:36 last edited by mpergand 3 Jul 2022, 15:52
                  #13

                  @Swati777999 said in Type Conversion : from QString to float and double:

                  QString myValue = "313.567889";
                  qDebug()<<" value after converting to float=" << myValue.toFloat(); // Output - 313.568
                  qDebug()<<" value after converting to double=" << myValue.Double(); // Output - 313.568

                  Strange result from QDebug ...

                  QString myValue = "313.567889";
                  qDebug()<<QString::asprintf("float =  %f",myValue.toFloat());
                  qDebug()<<QString::asprintf("double = %f",myValue.toDouble());
                  
                  "float =  313.567902"
                  "double = 313.567889"
                  

                  You excedeed the float limits already !
                  Doubles are better than float but have its own limits also:

                  qDebug()<<QString::asprintf("float :  %f %f %f %f",3.140000f,30.140000f,300.140000f,3000.140000f);
                  qDebug()<<QString::asprintf("double : %f %f %f %f",3.140000,3000.140000,0.140000+30e9,0.140000+30e10);
                  
                  "float :  3.140000 30.139999 300.140015 3000.139893"
                  "double : 3.140000 3000.140000 30000000000.139999 300000000000.140015"
                  

                  Don't try to predict any result from floating point numbers, you will be wrong every time.

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    fcarney
                    wrote on 7 Mar 2022, 16:18 last edited by
                    #14

                    If you want better precision you might have to use a library. I found this post about boost. So you could possibly use the boost multiprecision library: https://stackoverflow.com/questions/28133826/c-decimal-arithmetic-libraries

                    C++ is a perfectly valid school of magic.

                    1 Reply Last reply
                    1
                    • J Offline
                      J Offline
                      JKSH
                      Moderators
                      wrote on 8 Mar 2022, 02:35 last edited by
                      #15

                      @Swati777999 said in Type Conversion : from QString to float and double:

                      I want the answer to be shown in a non-string format. So, I want all digits of string to be shown without losing any digit during display.

                      You have not explained why you want this.

                      • It is impossible to do it using float and double.
                      • It is possible to do it by using an external library (like @fcarney said), but then you need to write lots of extra code and it is probably not worth it!

                      So, first you should tell us why "I want the answer to be shown in a non-string format. So, I want all digits of string to be shown without losing any digit during display." Then we can discuss the best solution.

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

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        Kent-Dorfman
                        wrote on 8 Mar 2022, 03:14 last edited by
                        #16

                        I'm suprised how many "wrong" answers there are in this thread given that the respondents are generally smart folks. 64 bit ieee double has requisite bits available to represent the requested value to 6 decimal places. That of course does not negate the OPs misunderstanding about float point approximations.

                        The following program demonstrates that the requested output format is possible, but also insinuates that the default float/double formatting of QDebug isn't adequate to display more decimal places to the right.

                        #include <iomanip>
                        #include <iostream>
                        #include <sstream>
                        #include <string>
                        
                        template<class T> std::string FORMAT(const T v) {
                            using namespace std;
                            ostringstream out;
                            out << setprecision(10) << v;
                            return out.str();
                        };
                        
                        int main(int argc, char* argv[]) {
                            using namespace std;
                        
                            string number = "313.567889";
                            istringstream floatInput(number);
                            istringstream doubleInput(number);
                        
                            float floatVal = 0.0F;
                            floatInput >> floatVal;
                            double doubleVal = 0.0;
                            doubleInput >> doubleVal;
                        
                            cout << "number: " << number << endl;
                            cout << "float: " << FORMAT<float>(floatVal) << endl;
                            cout << "double: " << FORMAT<double>(doubleVal) << endl;
                        
                            return 0;
                        
                        
                        }
                        

                        workshop:~$ ./a.out
                        number: 313.567889
                        float: 313.5679016
                        double: 313.567889
                        workshop:~$

                        1 Reply Last reply
                        3

                        15/16

                        8 Mar 2022, 02:35

                        • Login

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