PDA

View Full Version : precision of float field in qtableview



mandal
11th September 2009, 12:35
I have a qtableview to display result of a sql query.I have problems with filelds that defined to have float data types.

Table is defined in database as:
table1(studentId int,
studentGrade int,
weight float)
Here is how my table in database look like:


studentId studentGrade weight
1 80 0.7
2 90 0.8

i fill qSqlQueryModel with query (select * from table1) and then set model to qtableview.
But float fields' precision is lost.
Here is how my QTabelView looks like:


studentId studentGrade weight
1 80 0.69999999
2 90 0.7999999

anybody knows how to fix this?? i just want to see 0.7 and 0.8 in my qtableview.

thanx in adnavce.

Boron
11th September 2009, 15:36
That is because floats and doubles cannot "exactly" store 0.7 or 0.8 (see Wikipedia: http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems).

Do you use QString::setNum( double n, char format = 'g', int precision = 6 ) for setting the string?
Play around with format an precision. precision rounds the number. So if you want 0.7 and not 0.70 or 0.69999 use precision = 1.

mstegehu
25th May 2011, 11:16
Did you solve this problem? I am having the same problem and converting to string is nice but bad for sorting and alignment.

dibfibo
24th March 2020, 22:33
Did you solve this problem? I am having the same problem and converting to string is nice but bad for sorting and alignment.

Later years, someone can help me?

d_stranz
25th March 2020, 01:23
someone can help me?

Help you do what? Be specific.