Qt - custom decimal point and thousand separator

后端 未结 6 1835
野趣味
野趣味 2021-02-13 23:52

How can I convert a number (double) to string, with custom decimal point and thousand separator chars?

I\'ve seen QLocale, but I don\'t want to choose the localization c

6条回答
  •  情书的邮戳
    2021-02-14 00:28

    pretty horrible, but got the job done

    double doubleNumber = 5234556.3545;
    int precision = 2;
    
    QString stringNumber = QString::number(doubleNumber, 'f', precision);
    
    for(int point = 0, i = (stringNumber.lastIndexOf('.') == -1 ? stringNumber.length() : stringNumber.lastIndexOf('.')); i > 0; --i, ++point)
    {
        if(point != 0 && point % 3 == 0)
        {
            stringNumber.insert(i, ',');
        }
    }
    

提交回复
热议问题