Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. Problem to convert Float or Double to String in QLineEdit

Problem to convert Float or Double to String in QLineEdit

Scheduled Pinned Locked Moved Installation and Deployment
6 Posts 3 Posters 8.4k 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.
  • H Offline
    H Offline
    hammerfreedom
    wrote on last edited by
    #1

    Dear community, I have make an operation to calculate but when the amount is more than 1 million, the QLineEdit don't show all the number. Example : 1000000 = 1e+06.
    This is the code I have written

    @
    void PersoInfo::on_CalculOp_clicked()
    {
    //Permet de calculer une variable float et afficher le resultat
    //Proceder convertir String en float et enfin float en String
    QString str1,str2,str4,str5,str7,str8;

         str1=ui->txt_Prix_Salle->text();
         str2=ui->txt_Duree_Salle->text();
         str4=ui->txt_Prix_Chambre->text();
         str5=ui->txt_Duree_Chambre->text();
         str7=ui->txt_Prix_Restau->text();
         str8=ui->txt_Qte_Restau->text();
    
        float Csal,Ccha,Cres,Net,Tva;
         float Psal = str1.toFloat();
         float Dsal = str2.toFloat();
         float Pcha = str4.toFloat();
         float Dcha = str5.toFloat();
         float Pres = str7.toFloat();
         float Qres = str8.toFloat();
    
    
         Csal = Psal * Dsal;
         Ccha = Pcha * Dcha;
         Cres = Pres * Qres;
         Net = Csal + Ccha + Cres ;
         Tva = ((Net * 18) / 100);
    
         QString str3 = QString::number(Csal);
         ui->txt_Total_Salle->setText(str3);
    
         QString str6 = QString::number(Ccha);
         ui->txt_Total_Chambre->setText(str6);
    
         QString str9 = QString::number(Cres);
         ui->txt_Total_Restau->setText(str9);
    
         QString str0 = QString::number(Net);
         ui->txt_Net_Caisse->setText(str0);
    
         QString str10 = QString::number(Tva);
         ui->txt_Tva_Caisse->setText(str10);
    

    }
    @

    Thanks to help me

    [edit: added missing coding tags @ SGaist]

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      Please encapsulate the code inside the codetags "@@". Otherwise its difficult to understand the code.

      While converting to QString from number you can use "this":http://qt-project.org/doc/qt-5/qstring.html#number-2 overloaded function to display according to your need.
      So to display a million try this:
      @
      float num = 1000000;
      ui->lineEdit->setText(QString::number(num,'g',7));
      @

      Just set the proper precision as per the input number.

      157

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        Why don't you use QSpinBox or QDoubleSpinBox ? This way you don't have to check that your user enter anything else than numbers and you don't need to do all these conversions

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • H Offline
          H Offline
          hammerfreedom
          wrote on last edited by
          #4

          Please tell me How to use QSpinBox or QDoubleSpinBox. And I have a second problem, my application is saving in Sqlite Database and would like to write code to export the database in CSV and also Print my QLineEdit present on my Widget. How can I do that? Please Help Me

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Please, first read the documentation about these two classes. Then follow the QtSQL module for database related stuff and QtPrint for printing.

            The CSV format is pretty simple, you will need QFile and maybe QTextStream to simplify things a bit.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • H Offline
              H Offline
              hammerfreedom
              wrote on last edited by
              #6

              Ok, thanks for the tips

              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