QString:number with maximum 2 decimal places without trailing zero
Asked Answered
K

3

29

I have a division like this:

number / 1000.0

Sometimes it gives answers like 96.0000000001, sometimes the division works as expected.

I want to limit my number to a maximum of two decimal places and without trailing zeros.

If it's 96.5500000001 it should show 96.55.

If it's 96.4000000001 it should show 96.4

It is possible to format a string in this way?

I've checked the documentation and it provides 'f' argument for specifying the number of the decimal places but this way the trailing zeros remain. This is what I have tried:

QString::number(number / 1000.0, 'f', 2)

But this gives me for 96.4000000001 --> 96.40 instead of 96.4

Any solution? How can I format in this way?

Koss answered 22/7, 2014 at 8:49 Comment(0)
G
31

The documentation is pretty clear about what you should do:

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).

Therefore, use either the 'g' or 'G' format.

main.cpp

#include <QString>
#include <QDebug>

int main()
{
    qDebug() << QString::number(96400.0000001 / 1000.0, 'g', 5);
    qDebug() << QString::number(96550.0000001 / 1000.0, 'G', 5);
    return 0;
}

main.pro

TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp

Build and Run

qmake && make && ./main

Output

"96.4"
"96.55"
Glacial answered 22/7, 2014 at 9:1 Comment(3)
In your example if I do QString::number(1234.000001 / 1000.0, 'g', 5) it will return 1.234 and that's three decimals.Nole
Sure, so: 1) Truncate everything to maximum two digits - just like in your question already except a toDouble() call probably -, and then run my solution, and you will get more or less the corner case you want. You will still have the concise format unfortunately, admittedly. If that bugs you, call toDouble() again and use another format with the now chopped number. :)Pectoral
I appreciate you acknowledging the remaining issue, but it's not solved until that issue is solved. The problem with this answer is that the g option can leave you with scientific notation for many of your values, depending on your values. If you want to force fixed notation with variable decimals, you need to do different math and use the f option with variable precision values. You also need to cast to int if it's N.00 I just posted a full solution in a new answer.Kemerovo
K
5

This returns the formatted number always in fixed (not scientific) notation, and is reasonably efficient:

QString variableFormat(qreal n) { // assumes max precision of 2
    int i = rint(n * 100.0);
    if (i % 100)
        return QString::number(n, 'f', i % 10 ? 2 : 1);
    else
        return QString::number(i / 100);
}
Kemerovo answered 17/2, 2020 at 17:20 Comment(0)
E
4

Use QString::number with both 'f' and 'g'. First, use toDouble() on output of 'f'; then use QString::number with default arguments or with 'g' like this.

QString::number(QString::number(number / 1000.0, 'f', 2).toDouble(), 'g', 10);
Exurbanite answered 3/2, 2022 at 12:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.