Important: Please read the Qt Code of Conduct -

Problem to convert Float or Double to String in QLineEdit

  • 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;

        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);
         QString str6 = QString::number(Ccha);
         QString str9 = QString::number(Cres);
         QString str0 = QString::number(Net);
         QString str10 = QString::number(Tva);


    Thanks to help me

    [edit: added missing coding tags @ SGaist]

  • Moderators


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

    While converting to QString from number you can use "this": overloaded function to display according to your need.
    So to display a million try this:
    float num = 1000000;

    Just set the proper precision as per the input number.

  • Lifetime Qt Champion

    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

  • 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

  • Lifetime Qt Champion

    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.

  • Ok, thanks for the tips

Log in to reply