Results 1 to 17 of 17

Thread: use QString::number ( double n, char format = 'g', int precision = 6 )

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Posts
    42
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Question use QString::number ( double n, char format = 'g', int precision = 6 )

    hi, i find a very strange thing, i use QString::number ( double n, char format = 'g', int precision = 6 ) to show a double type data, the program is run in PC normal, but it run in ARM board, it show -0, and then the program is die, the cpu is up to 85%. but when i use QString::number ( int n, int base = 10 ) , is mean show int type data, the program run in PC or ARM board is well. this is why, and have any method to show double type. thanks.

  2. #2
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: use QString::number ( double n, char format = 'g', int precision = 6 )

    Most likely you have the wrong version of code somewhere and there's a mismatch in terms of the number of physical words of parms passed for the double value.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: use QString::number ( double n, char format = 'g', int precision = 6 )

    What does your code actually look like? Where has the double value come from? Does this work as expected:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. int main (int argc, char *argv[])
    5. {
    6. QCoreApplication app(argc, argv);
    7. double value = 3.14159265;
    8. qDebug() << QString::number(value, 'g', 6);
    9. return 0;
    10. }
    To copy to clipboard, switch view to plain text mode 
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  4. #4
    Join Date
    Mar 2011
    Posts
    42
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: use QString::number ( double n, char format = 'g', int precision = 6 )

    yea, my code is like with you, but the value is not expected, it will change everytime,
    this is my code
    Qt Code:
    1. int fValue; //fValue will changed in other place
    2. QString mStrContext;
    3. mStrContext = QString::number(fValue/10.0, 'g', 3);
    4. pp.drawText(x, y, mStrContext);
    To copy to clipboard, switch view to plain text mode 

    when i use qDebug() print the (fValue/10.0) is also be die.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: use QString::number ( double n, char format = 'g', int precision = 6 )

    Is your program crashing or are you just getting unexpected results? If the program is crashing then it is very unlikely that QString::number is involved.

    If the value you get out each time is different then it is likely that fValue is not being initialised before you use it (it isn't in your example).

    Compile and run my test program on your embedded system. Does it print:
    Qt Code:
    1. "3.14159"
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: use QString::number ( double n, char format = 'g', int precision = 6 )

    Depending on the compiler used, "fValue/10.0" will compile to different sized values. Probably technically a bug, but the compiler writer will tell you it's a "feechure". Assign the result to a temp and use the temp:
    Qt Code:
    1. double tempDbl = fValue/10.0;
    2. mStrContext = QString::number(tempDbl, 'g', 3);
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by lzpmail View Post
    yea, my code is like with you, but the value is not expected, it will change everytime,
    this is my code
    Qt Code:
    1. int fValue; //fValue will changed in other place
    2. QString mStrContext;
    3. mStrContext = QString::number(fValue/10.0, 'g', 3);
    4. pp.drawText(x, y, mStrContext);
    To copy to clipboard, switch view to plain text mode 

    when i use qDebug() print the (fValue/10.0) is also be die.
    (Actually, it probably isn't technically a compiler bug. "fValue/10.0" can legally compile to a float, not a double.)

  7. #7
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: use QString::number ( double n, char format = 'g', int precision = 6 )

    Nope you are wrong. C standard says that literal like "10.0" is treated as a double number so you have int/double and this is a double.
    Another thing is prototype of QString::number, the only floating point version is version with double (not with qreal) so should work same on ARM as on other platforms.
    Like I suggested until program wan't be debugged on arm or some unit test written which proves error in Qt I would assume bug in application not in Qt.

    Or maybe you (lzpmail) should give us wider context of usage.

  8. #8
    Join Date
    Mar 2011
    Posts
    42
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: use QString::number ( double n, char format = 'g', int precision = 6 )

    thanks your help, this probram is solved, but i use another method to solve this probram(use sprintf function to change double to char *), but i will continue find the reason, if i find i will tell yours.

  9. #9
    Join Date
    Mar 2011
    Posts
    42
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Talking QString::number ( double n, char format = 'g', int precision = 6 )title talk again

    hi, before i paste QString::number ( double n, char format = 'g', int precision = 6 ), this problem may have solved, before i use qt 4.5 to compile my program, have problem in ARM board, now i use qt 4.7 to compile, no that problem.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString::number ( double n, char format = 'g', int precision = 6 )title talk agai

    Is there a question here somewhere?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QString::number ( double n, char format = 'g', int precision = 6 )title talk agai

    No, I don't think so. lzpmail is just sharing the happy news that the problem from here has been solved.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString::number ( double n, char format = 'g', int precision = 6 )title talk agai

    Threads merged then.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Print floating and double precision number
    By marc2050 in forum Newbie
    Replies: 2
    Last Post: 17th May 2011, 08:15
  2. Replies: 7
    Last Post: 28th December 2010, 20:27
  3. The precision range of double
    By nikhilqt in forum Qt Programming
    Replies: 14
    Last Post: 8th July 2009, 15:08
  4. strange problem about QString::number(double)
    By yuzr in forum Qt for Embedded and Mobile
    Replies: 4
    Last Post: 24th December 2007, 13:05
  5. double precision or what?
    By mickey in forum General Programming
    Replies: 7
    Last Post: 20th February 2007, 20:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.