Unsolved Convert number
-
QString::number(number, 'f', 2)
When I put 1.3, 1, 1.1111
the console show: 1.30 1.00, 1.11
BUT How to remove the last 0 number: 1.3, 1, 1.11 -
@Kien-Bui
You can remove usingQRegExp("0+$")
.
In case of "1" you can useQRegExp("\\.$")
. -
@Kien-Bui Why do you want to remove it? It is doing exactly what you asked it to do: you want 2 digits after the dot, so it fills with 0.
-
@Ratzz We can don't use QString::number(number, 'f', 2)
I only need input & output exactly -
-
@Ratzz I don't want to use two RegExp for this.
I hope digit number after the dot, max is 2 or end of digit != 0 -
@Kien-Bui said in Convert number:
I don't want to use two RegExp for this.
Any problem with it?
I hope digit number after the dot, max is 2 or end of digit != 0
I still did not understand what u mean?
-
I got a answer: QString::number(number, 'f', 2).remove(QRegExp("\\.*0*$"))
-
int guessPrecision(double val, int maxdigits = 20){ double junk; int precision = 0; for(;!qFuzzyIsNull(std::modf(val,&junk)) && precision<maxdigits ;++precision) val*=10.0; return precision; }
QString::number(number, 'f', guessPrecision(number,2))