Important: Please read the Qt Code of Conduct -

How to get the double value converted to QString retaining value

  • hello,

    i have a question where i have a double value if , i try to convert to QString , the value entered will not be the same , i require the same number after converting to QString .

    i have a sample program
    double val = 459351301240397001;
    qDebug () << "Double value :" <<val<<endl;

    quint64 valint = val;
    qDebug () <<"qint value : "<<valint<<endl;
    QString valuestring = QString::number(valint);
    qDebug () <<"string value : "<<valuestring<<endl; 

    the output i got is :

    Double value : 4.59351e+17

    qint value : 459351301240396992

    string value : "459351301240396992"

    but i want the same number
    double val = 459351301240397001;
    when converted to QString. please help me out.

  • Moderators

    Floating point types, like float or double, are a terrible way to store large integer numbers.

    Your problem is not conversion to string. The double value is already stored skewed because of a limited precision at that order of magnitude. If it's an integer (like 459351301240397001) then strore it as such , and don't convert it to a floating point variable, as a loss of precision is inevitable in that case. If you need to store numbers even larger than that you'll need to use a library providing such types, e.g. GMP or TTMath.

  • @Chris Kawa

    I have prepended char value to double value and while getting the output i am removing the prepended value. so i have retained the original value.


Log in to reply