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
Forum Updated to NodeBB v4.3 + New Features

Rounding a real number to nth place of decimal

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 7 Posters 8.0k Views 1 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.
  • 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
                    • S SimonSchroeder

                      @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 Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #21

                      @SimonSchroeder nice catch, that explains why it doesn't collapse down to 314.195 but to 314.19


                      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 SimonSchroeder

                        @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.

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

                        @SimonSchroeder
                        Nice observation , Simon! This is where I made the mistake. Now, I'm getting values as 314.190 ,314.194533 which are the expected values.

                        Thanks for pointing it out,otherwise I would've continued to question my own coding ability.

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

                        1 Reply 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!

                          A Offline
                          A Offline
                          Alden_Massey
                          Banned
                          wrote on last edited by
                          #23
                          This post is deleted!
                          1 Reply Last reply
                          0

                          • Login

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