Convert a double to a QString

后端 未结 5 701
北荒
北荒 2021-02-02 05:39

I am writing a program in QT. I want to convert a double into a Qstring in C++.

5条回答
  •  独厮守ぢ
    2021-02-02 05:47

    Building on @Kristian's answer, I had a desire to display a fixed number of decimal places. That can be accomplished with other arguments in the QString::number(...) function. For instance, I wanted 3 decimal places:

    double value = 34.0495834;
    QString strValue = QString::number(value, 'f', 3);
    // strValue == "34.050"
    

    The 'f' specifies decimal format notation (more info here, you can also specify scientific notation) and the 3 specifies the precision (number of decimal places). Probably already linked in other answers, but more info about the QString::number function can be found here in the QString documentation

提交回复
热议问题