Display double number without exponential format



  • Hello! Let's say that I have a double number: 1679616. When I try to set text to label, the number is shown as 1.67962e+06. Is there a way so that the number can be shown as it is without exponential format? If I use: QString::number(result, 'f', 2) to cast the number to string, everything is ok, but what if the number has more than two digits after that point? I know I can set it to be 10 for example, but you know that this is not the right thing that I want to do. I want if the number is 82348.12323 to be shown 82348.12323. If it is 234.2, I want just one digit after the point.


  • Moderators

    Did you try this here?
    @
    QString::number(result,'f',-1);
    @



  • Thanks for the reply. I tried it but it shows six digits after the decimal point. Just like the default parameter if I haven't typed anything as third parameter.


  • Moderators

    [quote]
    Argument Formats
    In member functions where an argument format can be specified (e.g., arg(), number()), the argument format can be one of the following:
    Format Meaning
    e format as [-]9.9e[+|-]999
    E format as [-]9.9E[+|-]999
    f format as [-]9.9
    g use e or f format, whichever is the most concise
    G use E or f format, whichever is the most concise
    A precision is also specified with the argument format. For the 'e', 'E', and 'f' formats, the precision represents the number of digits after the decimal point. For the 'g' and 'G' formats, the precision represents the maximum number of significant digits (trailing zeroes are omitted).
    [/quote]
    The last sentence of section in "QString description":http://qt-project.org/doc/qt-5/QString.html#argument-formats says it. You should just use 'g' or 'G'.

    [edit, koahnig]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.