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. Rounding a real number to nth place of decimal
QtWS25 Last Chance

Rounding a real number to nth place of decimal

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 7 Posters 7.2k 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.
  • Swati777999S Offline
    Swati777999S Offline
    Swati777999
    wrote on last edited by Swati777999
    #1

    Hi all,

    I want to know the syntax for rounding a real number to nth decimal place.

    Thanks in advance!

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

    KroMignonK A 2 Replies Last reply
    0
    • J.HilkJ J.Hilk

      it's rather pointless, but here you go

      double roundToNthPlace(double value, int n){
              if (n < 1)
                  return static_cast<double>(qRound64(value));
              
              value *=  10 * n;
              value = qRound(value);
              value /= 10 * n;
              return value;
      }
      

      if you're fine with truncating instead of "rounding", than I would suggest using
      https://doc.qt.io/qt-5/qstring.html#number-6
      or
      https://doc.qt.io/qt-5/qstring.html#arg-9

      just before/for displaying the value.

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

      @J-Hilk

      This is the code I wrote with Qt functions

             QString myStr = "314.19";
             QString myStr1 = "314.1945327743682";
             int dec_pl=6;
             qDebug() << QString::number(1.125, 'f', 2);
             qDebug() << "Rounding 314.19 to 3 decimal places = " << QString::number(myStr.toDouble(), 'f', 3);
             qDebug() << "Rounding 314.1945327743682 to 6 decimal places = " << QString::number(myStr.toDouble(), 'f',dec_pl);
      

      trial-3(a)_02.03.2022png.png
      Why is the third result incorrect?

      -----------------------------------------------------------------------------------------------------------
      

      Edit:
      the third result is incorrect as I wrote

         qDebug() << "Rounding 314.1945327743682 to 6 decimal places = " << QString::number(myStr.toDouble(), 'f',dec_pl);
      

      so I was doing operation on myStr instead of myStr1

      So the correction is

          QString myStr = "314.19";
          QString myStr1 = "314.1945327743682";
          int dec_pl=6;
          qDebug() << QString::number(1.125, 'f', 2);
          qDebug() << "Rounding 314.19 to 3 decimal places = " << QString::number(myStr.toDouble(), 'f', 3);
          qDebug() << "Rounding 314.1945327743682 to 6 decimal places = " << QString::number(myStr1.toDouble(), 'f', dec_pl);
      

      which yields following result
      rounding_logic_02.03.png

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

      J.HilkJ S 2 Replies Last reply
      0
      • Swati777999S Swati777999

        Hi all,

        I want to know the syntax for rounding a real number to nth decimal place.

        Thanks in advance!

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

        @Swati777999 said in Rounding a float value to nth place of decimal:

        I want to know the syntax for rounding a real number to nth decimal place.

        What do you mean with this?
        Do you want to create a string representation of the real number?

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

        Swati777999S 1 Reply Last reply
        0
        • KroMignonK KroMignon

          @Swati777999 said in Rounding a float value to nth place of decimal:

          I want to know the syntax for rounding a real number to nth decimal place.

          What do you mean with this?
          Do you want to create a string representation of the real number?

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

          @KroMignon
          suppose I want to round pi to 3 decimal places or 4 decimal places. How can I achieve it in qt?

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

          KroMignonK 1 Reply Last reply
          0
          • Swati777999S Swati777999

            @KroMignon
            suppose I want to round pi to 3 decimal places or 4 decimal places. How can I achieve it in qt?

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

            @Swati777999 said in Rounding a float value to nth place of decimal:

            suppose I want to round pi to 3 decimal places or 4 decimal places. How can I achieve it in qt?

            This do not reply to my question, what is the purpose of this question?

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

            Swati777999S 1 Reply Last reply
            0
            • KroMignonK KroMignon

              @Swati777999 said in Rounding a float value to nth place of decimal:

              suppose I want to round pi to 3 decimal places or 4 decimal places. How can I achieve it in qt?

              This do not reply to my question, what is the purpose of this question?

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

              @KroMignon said in Rounding a float value to nth place of decimal:

              @Swati777999 said in Rounding a float value to nth place of decimal:

              suppose I want to round pi to 3 decimal places or 4 decimal places. How can I achieve it in qt?

              This do not reply to my question, what is the purpose of this question?

              What's the equivalent code in Qt for round(pi ,3) where pi =3.1415..... and round(real value,number of decimal places rounded to ) ?

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

              KroMignonK 1 Reply Last reply
              0
              • Swati777999S Swati777999

                @KroMignon said in Rounding a float value to nth place of decimal:

                @Swati777999 said in Rounding a float value to nth place of decimal:

                suppose I want to round pi to 3 decimal places or 4 decimal places. How can I achieve it in qt?

                This do not reply to my question, what is the purpose of this question?

                What's the equivalent code in Qt for round(pi ,3) where pi =3.1415..... and round(real value,number of decimal places rounded to ) ?

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

                @Swati777999 said in Rounding a float value to nth place of decimal:

                What's the equivalent code in Qt for round(pi ,3) where pi =3.1415..... and round(real value,number of decimal places rounded to ) ?

                This question don't made sense to me. What round() function are you talking about? In which programming language?
                Again, why do you want to round the real value? What is the use case?
                Do you want to create a string which contains the rounded 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)

                Swati777999S 1 Reply Last reply
                0
                • KroMignonK KroMignon

                  @Swati777999 said in Rounding a float value to nth place of decimal:

                  What's the equivalent code in Qt for round(pi ,3) where pi =3.1415..... and round(real value,number of decimal places rounded to ) ?

                  This question don't made sense to me. What round() function are you talking about? In which programming language?
                  Again, why do you want to round the real value? What is the use case?
                  Do you want to create a string which contains the rounded value?

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

                  @KroMignon said in Rounding a float value to nth place of decimal:

                  @Swati777999 said in Rounding a float value to nth place of decimal:

                  What's the equivalent code in Qt for round(pi ,3) where pi =3.1415..... and round(real value,number of decimal places rounded to ) ?

                  This question don't made sense to me. What round() function are you talking about? In which programming language?

                  round() of Python.

                  Again, why do you want to round the real value? What is the use case?

                  I want to round the real value to specific decimal places because I don't want to store other digits in my database.
                  Use cases can be any real number like round(54.3467,2) = 54.35 , round(5.842) = 6 , round(8.54123,4)=8.5412 ..so on

                  Do you want to create a string which contains the rounded value?

                  Yes. It does not matter which format but I want to be able to see the correct result on my screen.

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

                  KroMignonK JonBJ 2 Replies Last reply
                  0
                  • Swati777999S Swati777999

                    @KroMignon said in Rounding a float value to nth place of decimal:

                    @Swati777999 said in Rounding a float value to nth place of decimal:

                    What's the equivalent code in Qt for round(pi ,3) where pi =3.1415..... and round(real value,number of decimal places rounded to ) ?

                    This question don't made sense to me. What round() function are you talking about? In which programming language?

                    round() of Python.

                    Again, why do you want to round the real value? What is the use case?

                    I want to round the real value to specific decimal places because I don't want to store other digits in my database.
                    Use cases can be any real number like round(54.3467,2) = 54.35 , round(5.842) = 6 , round(8.54123,4)=8.5412 ..so on

                    Do you want to create a string which contains the rounded value?

                    Yes. It does not matter which format but I want to be able to see the correct result on my screen.

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

                    @Swati777999 said in Rounding a float value to nth place of decimal:

                    I want to round the real value to specific decimal places because I don't want to store other digits in my database.
                    Use cases can be any real number like round(54.3467,2) = 54.35 , round(5.842) = 6 , round(8.54123,4)=8.5412 ..so on

                    This question still looks strange and I am not sure you are really understanding what you are doing.
                    If you only what to read the value in your DB rounded to a specific decimal, why not using the SQL ROUND() function?

                    SELECT ROUND(floatValue, 2) as RoundedValue from myTable;
                    

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

                    Swati777999S 1 Reply Last reply
                    4
                    • KroMignonK KroMignon

                      @Swati777999 said in Rounding a float value to nth place of decimal:

                      I want to round the real value to specific decimal places because I don't want to store other digits in my database.
                      Use cases can be any real number like round(54.3467,2) = 54.35 , round(5.842) = 6 , round(8.54123,4)=8.5412 ..so on

                      This question still looks strange and I am not sure you are really understanding what you are doing.
                      If you only what to read the value in your DB rounded to a specific decimal, why not using the SQL ROUND() function?

                      SELECT ROUND(floatValue, 2) as RoundedValue from myTable;
                      
                      Swati777999S Offline
                      Swati777999S Offline
                      Swati777999
                      wrote on last edited by
                      #9

                      @KroMignon

                      I am looking for the logic of coding in qt not in SQL or Python. I've used the above examples just to show the desired output.

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

                      KroMignonK 1 Reply Last reply
                      0
                      • Swati777999S Swati777999

                        @KroMignon

                        I am looking for the logic of coding in qt not in SQL or Python. I've used the above examples just to show the desired output.

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

                        @Swati777999 said in Rounding a float value to nth place of decimal:

                        I am looking for the logic of coding in qt not in SQL or Python. I've used the above examples just to show the desired output.

                        Sorry, but I am not the right personne to help you. I do not understand what you expect to have.
                        A float/double value in C/C++ will always be stored in mantis / exposant format (cf. https://en.wikipedia.org/wiki/Double-precision_floating-point_format), so rounding a float do not make sense to me.

                        Perhaps someone else could better understand what you want to do.

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

                        Swati777999S 1 Reply Last reply
                        0
                        • KroMignonK KroMignon

                          @Swati777999 said in Rounding a float value to nth place of decimal:

                          I am looking for the logic of coding in qt not in SQL or Python. I've used the above examples just to show the desired output.

                          Sorry, but I am not the right personne to help you. I do not understand what you expect to have.
                          A float/double value in C/C++ will always be stored in mantis / exposant format (cf. https://en.wikipedia.org/wiki/Double-precision_floating-point_format), so rounding a float do not make sense to me.

                          Perhaps someone else could better understand what you want to do.

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

                          @KroMignon said in Rounding a float value to nth place of decimal:

                          @Swati777999 said in Rounding a float value to nth place of decimal:

                          I am looking for the logic of coding in qt not in SQL or Python. I've used the above examples just to show the desired output.

                          Sorry, but I am not the right personne to help you. I do not understand what you expect to have.
                          A float/double value in C/C++ will always be stored in mantis / exposant format (cf. https://en.wikipedia.org/wiki/Double-precision_floating-point_format), so rounding a float do not make sense to me.

                          To be more clear, I have edited the heading of the question. I want to round a real number to the desired place of decimal.

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

                          KroMignonK 1 Reply Last reply
                          0
                          • Cobra91151C Offline
                            Cobra91151C Offline
                            Cobra91151
                            wrote on last edited by Cobra91151
                            #12

                            Hello, @Swati777999!

                            Please check out the qRound methods: https://doc.qt.io/qt-5/qtglobal.html#qRound
                            Also, feel free to check out C++ math functions ceil (rounds up) and floor (rounds down): https://stackoverflow.com/questions/19985397/qt-decimals-and-round-up-to-whole-numbers
                            I think, it could be useful in your case. Happy coding!

                            1 Reply Last reply
                            0
                            • Swati777999S Swati777999

                              @KroMignon said in Rounding a float value to nth place of decimal:

                              @Swati777999 said in Rounding a float value to nth place of decimal:

                              I am looking for the logic of coding in qt not in SQL or Python. I've used the above examples just to show the desired output.

                              Sorry, but I am not the right personne to help you. I do not understand what you expect to have.
                              A float/double value in C/C++ will always be stored in mantis / exposant format (cf. https://en.wikipedia.org/wiki/Double-precision_floating-point_format), so rounding a float do not make sense to me.

                              To be more clear, I have edited the heading of the question. I want to round a real number to the desired place of decimal.

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

                              @Swati777999 said in Rounding a real number to nth place of decimal:

                              To be more clear, I have edited the heading of the question. I want to round a real number to the desired place of decimal.

                              Again, this not clear because it do not made sense: float/double value are coded as fractional part + exponent is a 32 bit word for float and 64 bit word for double.
                              You cannot round up a float to another float at nth decimal, this is non sense.

                              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
                              • Swati777999S Swati777999

                                @KroMignon said in Rounding a float value to nth place of decimal:

                                @Swati777999 said in Rounding a float value to nth place of decimal:

                                What's the equivalent code in Qt for round(pi ,3) where pi =3.1415..... and round(real value,number of decimal places rounded to ) ?

                                This question don't made sense to me. What round() function are you talking about? In which programming language?

                                round() of Python.

                                Again, why do you want to round the real value? What is the use case?

                                I want to round the real value to specific decimal places because I don't want to store other digits in my database.
                                Use cases can be any real number like round(54.3467,2) = 54.35 , round(5.842) = 6 , round(8.54123,4)=8.5412 ..so on

                                Do you want to create a string which contains the rounded value?

                                Yes. It does not matter which format but I want to be able to see the correct result on my screen.

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by
                                #14

                                @Swati777999 said in Rounding a real number to nth place of decimal:

                                I want to round the real value to specific decimal places because I don't want to store other digits in my database.

                                Real/floating point numbers (e.g. float in Python) do not "store digits" for a number. Rather they store a fixed-size bit pattern (say 8 bytes) which approximates to the variety of real numbers which can actually be stored.

                                Some databases have a DECIMAL type which does store numbers with a fixed number of decimal places.

                                You would have to start by stating what your database is. And even then it may not be possible/easy to store exactly as you state, depending on the library you are using to interface with the database.

                                1 Reply Last reply
                                1
                                • J.HilkJ Offline
                                  J.HilkJ Offline
                                  J.Hilk
                                  Moderators
                                  wrote on last edited by J.Hilk
                                  #15

                                  it's rather pointless, but here you go

                                  double roundToNthPlace(double value, int n){
                                          if (n < 1)
                                              return static_cast<double>(qRound64(value));
                                          
                                          value *=  10 * n;
                                          value = qRound(value);
                                          value /= 10 * n;
                                          return value;
                                  }
                                  

                                  if you're fine with truncating instead of "rounding", than I would suggest using
                                  https://doc.qt.io/qt-5/qstring.html#number-6
                                  or
                                  https://doc.qt.io/qt-5/qstring.html#arg-9

                                  just before/for displaying the value.


                                  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.

                                  Swati777999S 2 Replies Last reply
                                  2
                                  • J.HilkJ J.Hilk

                                    it's rather pointless, but here you go

                                    double roundToNthPlace(double value, int n){
                                            if (n < 1)
                                                return static_cast<double>(qRound64(value));
                                            
                                            value *=  10 * n;
                                            value = qRound(value);
                                            value /= 10 * n;
                                            return value;
                                    }
                                    

                                    if you're fine with truncating instead of "rounding", than I would suggest using
                                    https://doc.qt.io/qt-5/qstring.html#number-6
                                    or
                                    https://doc.qt.io/qt-5/qstring.html#arg-9

                                    just before/for displaying the value.

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

                                    @J-Hilk

                                    This is the code I wrote with Qt functions

                                           QString myStr = "314.19";
                                           QString myStr1 = "314.1945327743682";
                                           int dec_pl=6;
                                           qDebug() << QString::number(1.125, 'f', 2);
                                           qDebug() << "Rounding 314.19 to 3 decimal places = " << QString::number(myStr.toDouble(), 'f', 3);
                                           qDebug() << "Rounding 314.1945327743682 to 6 decimal places = " << QString::number(myStr.toDouble(), 'f',dec_pl);
                                    

                                    trial-3(a)_02.03.2022png.png
                                    Why is the third result incorrect?

                                    -----------------------------------------------------------------------------------------------------------
                                    

                                    Edit:
                                    the third result is incorrect as I wrote

                                       qDebug() << "Rounding 314.1945327743682 to 6 decimal places = " << QString::number(myStr.toDouble(), 'f',dec_pl);
                                    

                                    so I was doing operation on myStr instead of myStr1

                                    So the correction is

                                        QString myStr = "314.19";
                                        QString myStr1 = "314.1945327743682";
                                        int dec_pl=6;
                                        qDebug() << QString::number(1.125, 'f', 2);
                                        qDebug() << "Rounding 314.19 to 3 decimal places = " << QString::number(myStr.toDouble(), 'f', 3);
                                        qDebug() << "Rounding 314.1945327743682 to 6 decimal places = " << QString::number(myStr1.toDouble(), 'f', dec_pl);
                                    

                                    which yields following result
                                    rounding_logic_02.03.png

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

                                    J.HilkJ S 2 Replies Last reply
                                    0
                                    • J.HilkJ J.Hilk

                                      it's rather pointless, but here you go

                                      double roundToNthPlace(double value, int n){
                                              if (n < 1)
                                                  return static_cast<double>(qRound64(value));
                                              
                                              value *=  10 * n;
                                              value = qRound(value);
                                              value /= 10 * n;
                                              return value;
                                      }
                                      

                                      if you're fine with truncating instead of "rounding", than I would suggest using
                                      https://doc.qt.io/qt-5/qstring.html#number-6
                                      or
                                      https://doc.qt.io/qt-5/qstring.html#arg-9

                                      just before/for displaying the value.

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

                                      @J-Hilk said in Rounding a real number to nth place of decimal:

                                      it's rather pointless, but here you go

                                      double roundToNthPlace(double value, int n){
                                              if (n < 1)
                                                  return static_cast<double>(qRound64(value));
                                              
                                              value *=  10 * n;
                                              value = qRound(value);
                                              value /= 10 * n;
                                              return value;
                                      }
                                      

                                      if you're fine with truncating instead of "rounding", than I would suggest using
                                      https://doc.qt.io/qt-5/qstring.html#number-6
                                      or
                                      https://doc.qt.io/qt-5/qstring.html#arg-9

                                      just before/for displaying the value.

                                      my value is in QString . When I converted value from QString to double using .toDouble() , it's giving incorrect result.

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

                                      1 Reply Last reply
                                      0
                                      • Swati777999S Swati777999

                                        @J-Hilk

                                        This is the code I wrote with Qt functions

                                               QString myStr = "314.19";
                                               QString myStr1 = "314.1945327743682";
                                               int dec_pl=6;
                                               qDebug() << QString::number(1.125, 'f', 2);
                                               qDebug() << "Rounding 314.19 to 3 decimal places = " << QString::number(myStr.toDouble(), 'f', 3);
                                               qDebug() << "Rounding 314.1945327743682 to 6 decimal places = " << QString::number(myStr.toDouble(), 'f',dec_pl);
                                        

                                        trial-3(a)_02.03.2022png.png
                                        Why is the third result incorrect?

                                        -----------------------------------------------------------------------------------------------------------
                                        

                                        Edit:
                                        the third result is incorrect as I wrote

                                           qDebug() << "Rounding 314.1945327743682 to 6 decimal places = " << QString::number(myStr.toDouble(), 'f',dec_pl);
                                        

                                        so I was doing operation on myStr instead of myStr1

                                        So the correction is

                                            QString myStr = "314.19";
                                            QString myStr1 = "314.1945327743682";
                                            int dec_pl=6;
                                            qDebug() << QString::number(1.125, 'f', 2);
                                            qDebug() << "Rounding 314.19 to 3 decimal places = " << QString::number(myStr.toDouble(), 'f', 3);
                                            qDebug() << "Rounding 314.1945327743682 to 6 decimal places = " << QString::number(myStr1.toDouble(), 'f', dec_pl);
                                        

                                        which yields following result
                                        rounding_logic_02.03.png

                                        J.HilkJ Offline
                                        J.HilkJ Offline
                                        J.Hilk
                                        Moderators
                                        wrote on last edited by
                                        #18

                                        @Swati777999 said in Rounding a real number to nth place of decimal:

                                        314.1945327743682

                                        it's not wrong, it's the closest representation of your decimal value that is possible with a double.

                                        It's the nature of computer science, you'll have to find a way fitting for your situation that deals/works around it.


                                        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.

                                        Swati777999S 1 Reply Last reply
                                        1
                                        • J.HilkJ J.Hilk

                                          @Swati777999 said in Rounding a real number to nth place of decimal:

                                          314.1945327743682

                                          it's not wrong, it's the closest representation of your decimal value that is possible with a double.

                                          It's the nature of computer science, you'll have to find a way fitting for your situation that deals/works around it.

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

                                          @J-Hilk said in Rounding a real number to nth place of decimal:

                                          @Swati777999 said in Rounding a real number to nth place of decimal:
                                          314.1945327743682

                                          But I 'm expecting to get the result of 314.194533. So, it means there are no functions present in Qt which would round to the given decimal digits as required. I've to write a program for that.

                                          it's not wrong, it's the closest representation of your decimal value that is possible with a double.

                                          It's the nature of computer science, you'll have to find a way fitting for your situation that deals/works around it.

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

                                          1 Reply Last reply
                                          0
                                          • Swati777999S Swati777999

                                            @J-Hilk

                                            This is the code I wrote with Qt functions

                                                   QString myStr = "314.19";
                                                   QString myStr1 = "314.1945327743682";
                                                   int dec_pl=6;
                                                   qDebug() << QString::number(1.125, 'f', 2);
                                                   qDebug() << "Rounding 314.19 to 3 decimal places = " << QString::number(myStr.toDouble(), 'f', 3);
                                                   qDebug() << "Rounding 314.1945327743682 to 6 decimal places = " << QString::number(myStr.toDouble(), 'f',dec_pl);
                                            

                                            trial-3(a)_02.03.2022png.png
                                            Why is the third result incorrect?

                                            -----------------------------------------------------------------------------------------------------------
                                            

                                            Edit:
                                            the third result is incorrect as I wrote

                                               qDebug() << "Rounding 314.1945327743682 to 6 decimal places = " << QString::number(myStr.toDouble(), 'f',dec_pl);
                                            

                                            so I was doing operation on myStr instead of myStr1

                                            So the correction is

                                                QString myStr = "314.19";
                                                QString myStr1 = "314.1945327743682";
                                                int dec_pl=6;
                                                qDebug() << QString::number(1.125, 'f', 2);
                                                qDebug() << "Rounding 314.19 to 3 decimal places = " << QString::number(myStr.toDouble(), 'f', 3);
                                                qDebug() << "Rounding 314.1945327743682 to 6 decimal places = " << QString::number(myStr1.toDouble(), 'f', dec_pl);
                                            

                                            which yields following result
                                            rounding_logic_02.03.png

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

                                            @Swati777999 said in Rounding a real number to nth place of decimal:

                                            Why is the third result incorrect?

                                            In the code you posted you are "rounding" myStr to 6 decimal places instead of myStr1. So, the computer does exactly what you ask it to do.

                                            BTW, "rounding" is something different than specifying the number of decimal places for output. The saved floating point number still has the same precision as before.

                                            J.HilkJ Swati777999S 2 Replies Last reply
                                            4

                                            • Login

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