Important: Please read the Qt Code of Conduct -

[SOLVED] QLineEdit, setNum(), How to show more than 6 decimal places of a number?

  • Hi!

    If I use a QLineEdit for the "output" of a double number, it does not show more than 6 decimal places.

    QRegExpValidator or QDoubleValidator can be used to set a validator to "input" a number in QLineEdit very well. Even more than 6 digits!

    If I use:

    lineedit->setMaxLength(2);  // then the lineEdit can be truncated to max. 2 characters to be shown;
    lineedit->setMaxLength(10); // then the lineEdit displays max. 10 characters, but stops after the 6th decimal place 
    //(e.g. 0.123456)

    Is there a possibility to tell QLineEdit to display up till 10 decimal places (e.g. 0.0123456789 or 1.0123456789 or...)?

    Any suggestions? Please share!

  • Moderators

    QLineEdit only displays text (QString).
    How are you setting the number to display? The truncation happens when you convert the number to text, not in the QLineEdit.

  • Hi @Chris-Kawa!


    I nearly solved it with your hint:

     lineedit->setText(resultString.setNum(result,'g', 9)); // it displays 8 decimal places
     lineedit->setText(resultString.setNum(result,'g', 12)); // it displays also 8 decimal places

    Therefore, with

    ...,'g', 9));

    it displays two more decimal places. But, it cuts it off after 8 even for

    ...,'g', 12));

    I am quite happy with this solution. Nevertheless, if someone has got a suggestion for displaying 10 decimal places, please post!

  • Moderators

    As described in the documentation you are interested in the 'f' format with the precision set. The precision param in the 'g' format specifies number of significant digits, not the number of digits after decimal point.

    double num = 0.123456789123456789;
    lineedit->setText(QString::number(num, 'f', 10));

  • Solved! It is working now!

    I had set:



    This has been limiting:

    ...,'g', 10));

    After I have removed it, it works!

    @Chris-Kawa thanks for the other post, but

    'g', 10

    works fine now! Thanks anyway!

Log in to reply