IMO, it's just like cout, qDebug has a default precison when print a float, and it happened to be 0.1. But it will not do any harm to your program, it's just an output, so what are u worrying about...?
IMO, it's just like cout, qDebug has a default precison when print a float, and it happened to be 0.1. But it will not do any harm to your program, it's just an output, so what are u worrying about...?
1. Users don't have the manual, and if they did, they wouldn't read it.
2. In fact, users can't read anything, and if they could, they wouldn't want to.
What I'm worried about is that my software checks to see if the customer is underpaying. You can't have the customer give the cashier a $5 bill to pay a $10 tab. The problem is that once the amount gets large enough, there can be up to a nickel of error. So, yesterday when a customer tried to make a $13125.20 purchase, and paid exactly $13125.20 from their credit card, the software thought that he wasn't paying enough because the variable storing the amount owed was actually $13125.24.Originally Posted by bood
I don't think this is just a problem with qDebug() truncating. I am rounding all of my float values to two significant digits (cents), and I use QString::number() all over my code. Now, it WILL format to just one digit if the final digit is a "0", but it doesn't just automatically truncate to .1 every time. Even qstring::number is seeing the value, which is 13125.24 as 13125.2
I considered the possibility that I am making a mistake (which of course I still may be), but if QString::number() were always truncating to .1 then my users CERTAINLY would have noticed because every total would always be even.
The docs say:Originally Posted by hardgeus
Here's a small test:QString QString::number ( double n, char f = 'g', int prec = 6 ) [static]
Output:Qt Code:
#include <QtDebug> #include <QString> int main() { qDebug() << "f"; qDebug() << "g"; }To copy to clipboard, switch view to plain text mode
As you can see everything depends on what you order QString::number() to do."13125.2"
f
"13125.2"
"13125.25"
"13125.246"
g
"13125.2"
"13125.25"
"13125.246"
That's the problem...What was happening is I was assigning the values I pulled from the database into my amount textbox using:Originally Posted by jacek
txtAmount->setText( QString::number(fValue) );
This was truncating my cents when the value got over 10,000! Then, later, when I pulled it out of the textbox, the value was wrong. I was using the qDebug() to print, and this was masking the error since it was also truncating the cents. This error ONLY occurred when values were over 10,000 (which is unusual in my app) so the problem rarely occurred.
I'm glad this was the problem. I was starting to think that I didn't understand the behavior of floating point numbers!
Try:and better don't use floating point numbers for large amounts of money.Qt Code:
if( fOwed > fPaid ) { printf("%.3f owed, %.3f paid\n", fOwed, fPaid); qDebug << fixed << fOwed << " owed " << fPaid << " paid"; }To copy to clipboard, switch view to plain text mode
Bookmarks