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. QString::number(14.24,'g',3) returns 14.3 ?
Forum Updated to NodeBB v4.3 + New Features

QString::number(14.24,'g',3) returns 14.3 ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.7k Views 2 Watching
  • 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.
  • Seb TurS Offline
    Seb TurS Offline
    Seb Tur
    wrote on last edited by
    #1

    I used QString::number(doubleNumber, 'g' , 3) to get String representing the significant decimals up to 3.
    It works well in 99.9999% cases but for 14.25 instead of giving 14.25 it returns 14.3
    Why is that and what to use instead?

    I know I could use QString::number(doubleNumber'f',3) and manually remove trailing zeros but why ?

    JonBJ 1 Reply Last reply
    0
    • Seb TurS Seb Tur

      I used QString::number(doubleNumber, 'g' , 3) to get String representing the significant decimals up to 3.
      It works well in 99.9999% cases but for 14.25 instead of giving 14.25 it returns 14.3
      Why is that and what to use instead?

      I know I could use QString::number(doubleNumber'f',3) and manually remove trailing zeros but why ?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Seb-Tur
      I don't know about your cases where you say it "works", but where does g format say it produces 3 significant decimals? It does not. See https://doc.qt.io/qt-6/qlocale.html#toString-8

      'g' use 'e' or 'f' format, whichever is more concise maximum number of significant digits (trailing zeroes are omitted)

      The precision is "maximum number of significant digits".

      Paul ColbyP 1 Reply Last reply
      1
      • JonBJ JonB

        @Seb-Tur
        I don't know about your cases where you say it "works", but where does g format say it produces 3 significant decimals? It does not. See https://doc.qt.io/qt-6/qlocale.html#toString-8

        'g' use 'e' or 'f' format, whichever is more concise maximum number of significant digits (trailing zeroes are omitted)

        The precision is "maximum number of significant digits".

        Paul ColbyP Offline
        Paul ColbyP Offline
        Paul Colby
        wrote on last edited by
        #3

        I used QString::number(doubleNumber, 'g' , 3) to get String representing the significant decimals up to 3.

        The g format doesn't give 3 "significant decimals", it gives 3 "significant digits" - this includes digits both before (excluding leading 0's) and after the decimal. Have a read of https://en.wikipedia.org/wiki/Significant_figures for more info.

        It works well in 99.9999% cases but for 14.25 instead of giving 14.25 it returns 14.3

        14.3 is correct. The three significant digits there are 1, 4, and .3.

        I know I could use QString::number(doubleNumber'f',3) ... Why is that

        The reason the f format gives more decimals in your example, is that the f format interprets the precision parameter as a number of decimals, whereas g interprets it as a number of significant digits (which, as I mentioned above, includes digits before the decimal too).

        This is documented in the table under QLocale::toString(), which QString::number() links to (and @JonB referenced above). See the "Meaining of precision column:

        7badb4e9-dd1b-439a-a46a-a0cdc90f91ed-image.png

        and what to use instead?

        If you specifically want "up to 3 decimals", as opposed to "exactly 3 decimals" (f) or "up to 3 significant digits" (g), then I suppose using f then trimming trailing 0 chars is one way to go.

        Just one of many ways to do it:

            const double doubleNumber = 14.25;
            qDebug() << QString::number(doubleNumber,'f',3);
            qDebug() << QString::number(doubleNumber,'g',3);
            qDebug() << QString::number(doubleNumber,'f',3).remove(QRegularExpression(QStringLiteral("0+$")));
        

        Outputs:

        "14.250"
        "14.3"
        "14.25"
        

        Cheers.

        1 Reply Last reply
        4

        • Login

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