PDA

View Full Version : How does QLocale::toString(double i, char f, int prec) round?



ChiliPalmer
10th August 2010, 12:16
Hi,

we store certain values with 4 decimal places in our database. In one method I need to convert the sum of two of these values to a QString with 2 decimal places using QLocale::toString. In another method I need to convert the same sum to an integer (= sum*100 with last two digits same as 2 decimal places in the QString).

Code used in the first method:
QLocale::system().toString(value1 + value2, 'f', 2);

Second method:
qRound((value1 + value2) * 100.00);

In most cases this works, but every once in a while there is a difference of 1 between the last digit in the integer and 2nd decimal place in the QString. So I guess QLocale::toString doesn't round exactly like qRound. I have taken a look at the source code, but couldn't find out more.
Any suggestions?


Edit:
Here are some samples where that problem occurs:

-
toString(37.485, 'f', 2) = 37,48
qRound(3748.5) = 3749

-
toString(169.575, 'f', 2) = 169,57
qRound(16957.5) = 16958

So does QLocale::toString generally just cut off the unnecessary decimal places without rounding?