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 to selected decimal places

Rounding to selected decimal places

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 2.5k 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

    I'm trying to create a small application that
    i) intakes any float value from user[in QLineEdit] ,
    ii) asks to select number of places to round that value [to select from QSpinBox]
    iii) show the output in QLineEdit

    Here's my code
    mainwindow.cpp

    #include "mainwindow.h"
    #include <QDebug>
    #include<QSpinBox>
    #include<QDebug>
    #include<QLineEdit>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
    
        float val=0.0;
        //QLineEdit *user_ins = QLineEdit("Enter the  values");   --> says wrong syntax
        qDebug() << "Enter a value" <<val;
    
        QSpinBox *sbx = new QSpinBox(this);
        // int a=this->x();   --> i want to shift the position of spinbox from its by default position which is (0,0)
        qDebug()<<a;
        sbx->setRange(0,10);
        sbx->setPrefix("Round to ");
        sbx->setSuffix(" decimal places");
        sbx->setFixedSize(200,100);
        int val_spin;
        val_spin =sbx->value();
        qDebug()<<val_spin;
    
     float output=qRound(val,val_spin);
    
    }
    
    

    Can someone suggest me how to achieve it?

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

    J.HilkJ 1 Reply Last reply
    0
    • Swati777999S Swati777999

      I'm trying to create a small application that
      i) intakes any float value from user[in QLineEdit] ,
      ii) asks to select number of places to round that value [to select from QSpinBox]
      iii) show the output in QLineEdit

      Here's my code
      mainwindow.cpp

      #include "mainwindow.h"
      #include <QDebug>
      #include<QSpinBox>
      #include<QDebug>
      #include<QLineEdit>
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
      {
      
          float val=0.0;
          //QLineEdit *user_ins = QLineEdit("Enter the  values");   --> says wrong syntax
          qDebug() << "Enter a value" <<val;
      
          QSpinBox *sbx = new QSpinBox(this);
          // int a=this->x();   --> i want to shift the position of spinbox from its by default position which is (0,0)
          qDebug()<<a;
          sbx->setRange(0,10);
          sbx->setPrefix("Round to ");
          sbx->setSuffix(" decimal places");
          sbx->setFixedSize(200,100);
          int val_spin;
          val_spin =sbx->value();
          qDebug()<<val_spin;
      
       float output=qRound(val,val_spin);
      
      }
      
      

      Can someone suggest me how to achieve it?

      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Swati777999

      • multiply by 10 * SelectedDecimal Places
      • check if the value overflowed, if yes, than use the original value
      • otherwise, round to integer, use your preferred method and divide the result by 10 * SelectedDecimal Places

      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.

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

        @Swati777999

        • multiply by 10 * SelectedDecimal Places
        • check if the value overflowed, if yes, than use the original value
        • otherwise, round to integer, use your preferred method and divide the result by 10 * SelectedDecimal Places
        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #3

        @J-Hilk
        While this is fine, may I ask why for simplicity you do not refer the user to e.g. QString QString::number(double n, char format = 'g', int precision = 6)?

        J.HilkJ 1 Reply Last reply
        0
        • JonBJ JonB

          @J-Hilk
          While this is fine, may I ask why for simplicity you do not refer the user to e.g. QString QString::number(double n, char format = 'g', int precision = 6)?

          J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @JonB said in Rounding to selected decimal places:

          @J-Hilk
          While this is fine, may I ask why for simplicity you do not refer the user to e.g. QString QString::number(double n, char format = 'g', int precision = 6)?

          because the op's expected output is a float, and I don't like needlessly converting to and from QString :D


          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.

          JonBJ 1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            @JonB said in Rounding to selected decimal places:

            @J-Hilk
            While this is fine, may I ask why for simplicity you do not refer the user to e.g. QString QString::number(double n, char format = 'g', int precision = 6)?

            because the op's expected output is a float, and I don't like needlessly converting to and from QString :D

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @J-Hilk said in Rounding to selected decimal places:

            because the op's expected output is a float, and I don't like needlessly converting to and from QString

            The OP writes:

            iii) show the output in QLineEdit

            J.HilkJ 1 Reply Last reply
            0
            • JonBJ JonB

              @J-Hilk said in Rounding to selected decimal places:

              because the op's expected output is a float, and I don't like needlessly converting to and from QString

              The OP writes:

              iii) show the output in QLineEdit

              J.HilkJ Online
              J.HilkJ Online
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #6

              @JonB oh I only focused on the titel and the code given 😅

              may bad


              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
              1
              • J.HilkJ J.Hilk

                @Swati777999

                • multiply by 10 * SelectedDecimal Places
                • check if the value overflowed, if yes, than use the original value
                • otherwise, round to integer, use your preferred method and divide the result by 10 * SelectedDecimal Places
                Swati777999S Offline
                Swati777999S Offline
                Swati777999
                wrote on last edited by Swati777999
                #7

                @J-Hilk

                with your suggestion multiply by 10

                MainWindow::MainWindow(QWidget *parent)
                    : QMainWindow(parent)
                {
                
                    float val=0.0;
                    // QLineEdit *user_ins = QLineEdit("Enter a value");  --> says wrong syntax
                    qDebug() << "Enter a value" <<val;
                
                    QSpinBox *sbx = new QSpinBox(this);
                    int a=this->x();
                    qDebug()<<a;
                    sbx->setRange(0,10);
                    sbx->setPrefix("Round to ");
                    sbx->setSuffix(" decimal places");
                    sbx->setFixedSize(200,100);
                    int val_spin;
                    val_spin =sbx->value();
                    val_spin*=10;   ----> multiplication by 10
                    qDebug()<<val_spin;
                    
                }
                

                I am not able to see the result ( no output in Application Output) . I want the result tobe displayed on the window.

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

                JonBJ 1 Reply Last reply
                0
                • Swati777999S Swati777999

                  @J-Hilk

                  with your suggestion multiply by 10

                  MainWindow::MainWindow(QWidget *parent)
                      : QMainWindow(parent)
                  {
                  
                      float val=0.0;
                      // QLineEdit *user_ins = QLineEdit("Enter a value");  --> says wrong syntax
                      qDebug() << "Enter a value" <<val;
                  
                      QSpinBox *sbx = new QSpinBox(this);
                      int a=this->x();
                      qDebug()<<a;
                      sbx->setRange(0,10);
                      sbx->setPrefix("Round to ");
                      sbx->setSuffix(" decimal places");
                      sbx->setFixedSize(200,100);
                      int val_spin;
                      val_spin =sbx->value();
                      val_spin*=10;   ----> multiplication by 10
                      qDebug()<<val_spin;
                      
                  }
                  

                  I am not able to see the result ( no output in Application Output) . I want the result tobe displayed on the window.

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #8

                  @Swati777999 said in Rounding to selected decimal places:

                  I am not able to see the result ( no output in Application Output) .

                  I do not believe this. You have qDebug()<<val_spin;; same for qDebug()<<a;. If you run under debugger/from Qt Creator you should see it there, try qDebug() << "Hello";, do you see that?

                  I want the result tobe displayed on the window.

                  What "window"? The Application Output pane?

                  In the long run you cannot use the Application Output pane and qDebug() statements to do your work.

                  QSpinBox *sbx = new QSpinBox(this);
                  ....
                  val_spin =sbx->value();

                  You cannot ask for a value from a widget as soon as you have created it, as your code does. Your code will simply get the spin box's initial value, probably 0. You have to show the widget and allow the user to interact with it, getting the value when the user has finished doing so. Your understanding/implementation of a UI with widgets and an event loop is missing.

                  Swati777999S 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @Swati777999 said in Rounding to selected decimal places:

                    I am not able to see the result ( no output in Application Output) .

                    I do not believe this. You have qDebug()<<val_spin;; same for qDebug()<<a;. If you run under debugger/from Qt Creator you should see it there, try qDebug() << "Hello";, do you see that?

                    I want the result tobe displayed on the window.

                    What "window"? The Application Output pane?

                    In the long run you cannot use the Application Output pane and qDebug() statements to do your work.

                    QSpinBox *sbx = new QSpinBox(this);
                    ....
                    val_spin =sbx->value();

                    You cannot ask for a value from a widget as soon as you have created it, as your code does. Your code will simply get the spin box's initial value, probably 0. You have to show the widget and allow the user to interact with it, getting the value when the user has finished doing so. Your understanding/implementation of a UI with widgets and an event loop is missing.

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

                    @JonB said in Rounding to selected decimal places:

                    @Swati777999 said in Rounding to selected decimal places:

                    I am not able to see the result ( no output in Application Output) .

                    I do not believe this. You have qDebug()<<val_spin;; same for qDebug()<<a;. If you run under debugger/from Qt Creator you should see it there, try qDebug() << "Hello";, do you see that?

                    I want the result tobe displayed on the window.

                    What "window"? The Application Output pane?

                    In the long run you cannot use the Application Output pane and qDebug() statements to do your work.

                    QSpinBox *sbx = new QSpinBox(this);
                    ....
                    val_spin =sbx->value();

                    You cannot ask for a value from a widget as soon as you have created it, as your code does. Your code will simply get the spin box's initial value, probably 0. You have to show the widget and allow the user to interact with it, getting the value when the user has finished doing so. Your understanding/implementation of a UI with widgets and an event loop is missing.

                    I am a beginner in Qt and from a non-IT/computer science background. I'm learning through various resources and of course through this platform.

                    I have used qDebug() at many places just to be sure at the intermediary steps if I'm going in the right direction.

                    By using a, I wanted to shift QSpinBox from its default position,i.e origin (0,0) of the window.
                    What's the correct approach for intaking values from users, showing them on the screen, rounding it through the value chosen through Spinbox, and displaying the desired result??

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

                    jsulmJ JonBJ 2 Replies Last reply
                    0
                    • Swati777999S Swati777999

                      @JonB said in Rounding to selected decimal places:

                      @Swati777999 said in Rounding to selected decimal places:

                      I am not able to see the result ( no output in Application Output) .

                      I do not believe this. You have qDebug()<<val_spin;; same for qDebug()<<a;. If you run under debugger/from Qt Creator you should see it there, try qDebug() << "Hello";, do you see that?

                      I want the result tobe displayed on the window.

                      What "window"? The Application Output pane?

                      In the long run you cannot use the Application Output pane and qDebug() statements to do your work.

                      QSpinBox *sbx = new QSpinBox(this);
                      ....
                      val_spin =sbx->value();

                      You cannot ask for a value from a widget as soon as you have created it, as your code does. Your code will simply get the spin box's initial value, probably 0. You have to show the widget and allow the user to interact with it, getting the value when the user has finished doing so. Your understanding/implementation of a UI with widgets and an event loop is missing.

                      I am a beginner in Qt and from a non-IT/computer science background. I'm learning through various resources and of course through this platform.

                      I have used qDebug() at many places just to be sure at the intermediary steps if I'm going in the right direction.

                      By using a, I wanted to shift QSpinBox from its default position,i.e origin (0,0) of the window.
                      What's the correct approach for intaking values from users, showing them on the screen, rounding it through the value chosen through Spinbox, and displaying the desired result??

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

                      @Swati777999 said in Rounding to selected decimal places:

                      What's the correct approach for intaking values from users

                      Signals/slots
                      https://doc.qt.io/qt-5/qspinbox.html#valueChanged

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

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

                        when sticking to my "solution":

                        int main(int argc, char *argv[])
                        {
                            QApplication a(argc, argv);
                        
                        
                            auto w = new QWidget;
                            QVBoxLayout *layout = new QVBoxLayout(w);
                        
                            auto lineEdit = new QLineEdit(w);
                            auto sBox = new QSpinBox(w);
                            sBox->setRange(0,10);
                            sBox->setPrefix("Round to");
                            sBox->setSuffix(" decimal places");
                            auto label = new QLabel(w);
                        
                            layout->addWidget(lineEdit);
                            layout->addWidget(sBox);
                            layout->addWidget(label);
                        
                            w->resize( 200, 50);
                            w->show();
                        
                            auto onStuffChanged =[=]()->void{
                                bool ok{false};
                                auto value = lineEdit->text().toDouble(&ok);
                        
                                if(!ok){
                                    label->setText("Invalid Input");
                                    return;
                                }
                        
                                if(auto decimal = sBox->value(); decimal == 0){
                                    label->setText(QString::number(qRound(value)));
                                } else {
                                    auto newValue = value * (10 * decimal);
                                    newValue = qRound(newValue);
                                    newValue /= (10 * decimal);
                                    label->setText(QString::number(newValue));
                                }
                            };
                        
                            QObject::connect(lineEdit, &QLineEdit::textChanged, [=]()->void{onStuffChanged();});
                            QObject::connect(sBox, QOverload<int>::of(&QSpinBox::valueChanged), [=]()->void{onStuffChanged();});
                        
                            return a.exec();
                        }
                        

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

                          @JonB said in Rounding to selected decimal places:

                          @Swati777999 said in Rounding to selected decimal places:

                          I am not able to see the result ( no output in Application Output) .

                          I do not believe this. You have qDebug()<<val_spin;; same for qDebug()<<a;. If you run under debugger/from Qt Creator you should see it there, try qDebug() << "Hello";, do you see that?

                          I want the result tobe displayed on the window.

                          What "window"? The Application Output pane?

                          In the long run you cannot use the Application Output pane and qDebug() statements to do your work.

                          QSpinBox *sbx = new QSpinBox(this);
                          ....
                          val_spin =sbx->value();

                          You cannot ask for a value from a widget as soon as you have created it, as your code does. Your code will simply get the spin box's initial value, probably 0. You have to show the widget and allow the user to interact with it, getting the value when the user has finished doing so. Your understanding/implementation of a UI with widgets and an event loop is missing.

                          I am a beginner in Qt and from a non-IT/computer science background. I'm learning through various resources and of course through this platform.

                          I have used qDebug() at many places just to be sure at the intermediary steps if I'm going in the right direction.

                          By using a, I wanted to shift QSpinBox from its default position,i.e origin (0,0) of the window.
                          What's the correct approach for intaking values from users, showing them on the screen, rounding it through the value chosen through Spinbox, and displaying the desired result??

                          JonBJ Online
                          JonBJ Online
                          JonB
                          wrote on last edited by
                          #12

                          @Swati777999
                          I understand, but you have made a lot of posts. By now you must know the basics of how to write a Qt UI program and have it interact properly. You are asking me to write the basics of how any Qt UI works, and I don't have time to do that.

                          Do you understand why

                          QSpinBox *sbx = new QSpinBox(this);
                          ....
                          val_spin =sbx->value();
                          

                          is not going to allow the user to interact with the spin box to enter a value before you read it back?

                          qDebug() << "Enter a value" <<val;

                          The end user is not going to see this. Presumably you want to put that on a QLabel next to the QSpinBox?

                          int a=this->x();

                          This does nothing. If you want to move the widget --- and you are not using layouts as you ought to be --- use void QWidget::move(int x, int y).

                          You have created a spin box for the user to enter how many decimal places desired (val_spin). But there is no widget for the user to enter the float val=0.0 you have, so that stays at 0.0.

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

                            when sticking to my "solution":

                            int main(int argc, char *argv[])
                            {
                                QApplication a(argc, argv);
                            
                            
                                auto w = new QWidget;
                                QVBoxLayout *layout = new QVBoxLayout(w);
                            
                                auto lineEdit = new QLineEdit(w);
                                auto sBox = new QSpinBox(w);
                                sBox->setRange(0,10);
                                sBox->setPrefix("Round to");
                                sBox->setSuffix(" decimal places");
                                auto label = new QLabel(w);
                            
                                layout->addWidget(lineEdit);
                                layout->addWidget(sBox);
                                layout->addWidget(label);
                            
                                w->resize( 200, 50);
                                w->show();
                            
                                auto onStuffChanged =[=]()->void{
                                    bool ok{false};
                                    auto value = lineEdit->text().toDouble(&ok);
                            
                                    if(!ok){
                                        label->setText("Invalid Input");
                                        return;
                                    }
                            
                                    if(auto decimal = sBox->value(); decimal == 0){
                                        label->setText(QString::number(qRound(value)));
                                    } else {
                                        auto newValue = value * (10 * decimal);
                                        newValue = qRound(newValue);
                                        newValue /= (10 * decimal);
                                        label->setText(QString::number(newValue));
                                    }
                                };
                            
                                QObject::connect(lineEdit, &QLineEdit::textChanged, [=]()->void{onStuffChanged();});
                                QObject::connect(sBox, QOverload<int>::of(&QSpinBox::valueChanged), [=]()->void{onStuffChanged();});
                            
                                return a.exec();
                            }
                            
                            Swati777999S Offline
                            Swati777999S Offline
                            Swati777999
                            wrote on last edited by
                            #13

                            @J-Hilk

                            Thanks for your solution but QOverload seems to be creating trouble ;
                            stringRound-1_22.02.2022.PNG

                             auto onStuffChanged =[=]()->void 
                            

                            I don't understand the above notation [Is it in JavaScript ?] . Sorry for asking so basic questions as I am literally struggling with Qt and get overwhelmed to see stuff in code beyond my understanding. So sorry for troubling moderators; @JonB @jsulm with some of my basic doubts but for a self-developer like me, if I keep my doubts to myself, it won't do much benefit to me in a long run.

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

                            J.HilkJ 1 Reply Last reply
                            0
                            • Swati777999S Swati777999

                              @J-Hilk

                              Thanks for your solution but QOverload seems to be creating trouble ;
                              stringRound-1_22.02.2022.PNG

                               auto onStuffChanged =[=]()->void 
                              

                              I don't understand the above notation [Is it in JavaScript ?] . Sorry for asking so basic questions as I am literally struggling with Qt and get overwhelmed to see stuff in code beyond my understanding. So sorry for troubling moderators; @JonB @jsulm with some of my basic doubts but for a self-developer like me, if I keep my doubts to myself, it won't do much benefit to me in a long run.

                              J.HilkJ Online
                              J.HilkJ Online
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #14

                              @Swati777999 what version of Qt and c++ compiler are you using ?

                              I don't understand the above notation [Is it in JavaScript ?]

                              no it's c++ its a lambda.
                              https://en.cppreference.com/w/cpp/language/lambda
                              a "local function"

                              my example is a self containing minimal example, that fits completely into main.cpp. So I had to do some tricks and "advanced" technics, to make it all work. It's much less complex if you have an actual class with actual slots/functions to call/connect to.


                              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
                              3

                              • Login

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