qtqstring

qt convert number Not compliant with prefetching


I hope the value of str is 1.235. But in fact, the value is 1.234 by qt. Why? What happened in theory?

double d=1.2345;
auto str=QString::number(d,'f',3); 

Solution

  • As per the documentation, QString::number() doesn't do any rounding, just formatting. That is, rounding isn't mentioned anywhere that I can see. I would not expect it to do any rounding.

    To round a number to a certain number of decimals, one could use, for example (3 decimal places):

    double rounded = std::round(d * 1000.0) / 1000.0;