Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get the double value converted to QString retaining value

How to get the double value converted to QString retaining value

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Pradeep KumarP Offline
    Pradeep KumarP Offline
    Pradeep Kumar
    wrote on last edited by
    #1

    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.

    Pradeep Kumar
    Qt,QML Developer

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      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.

      1 Reply Last reply
      4
      • Pradeep KumarP Offline
        Pradeep KumarP Offline
        Pradeep Kumar
        wrote on last edited by
        #3

        @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.

        Thanks

        Pradeep Kumar
        Qt,QML Developer

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved