Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a column of float number in my SQL server table, before inserting data into that table, I use cast() and round() to truncate any digits which are too far from the decimal point, for example 7.6899999999999995 should be rounded and casted to float type as 7.69, the combination of these functions work perfectly and when I view all the rows from that table by query in SQL Management studio, all the values in the column of float type are OK, but when I load that table into a dataset and display data using a datagridview, there are some values becoming 'original' like as they had never been rounded, the value 7.69 in my database becomes 7.6899999999999995 in my datagridview, that's so ugly and not what I want.

Could you please give me any reason for this and suggest me with some solution? I'm stuck at this totally.

Your help would be highly appreciated!
Thanks!
Posted

1 solution

The trailing digits You don't want to appear are normal behavior with floating point numbers (specifically with single precision ones like "float" or "Single") due to their intrinsic rounding that lead them to store the value in an approximated form.
To avoid that akward representation, explicit rounding is needed.

To accomplish that, if You are using a DataGridViewTextBoxColumn then You have to replace DefaultCellStyle property or its Format sub-property;

For example:
C#
Column1.DefaultCellStyle.Format = "N2"; // number with two digits after point


You can change format directly from code or through the grid designer.

Regards,
Daniele.
 
Share this answer
 
Comments
supernorb 15-Jan-13 6:43am    
Thank you, you really helped me out. Could you give me more reference about this kind of format using for DefaultCellStyle.Format? I want to share with you a little nasty problem with your solution, in fact my datagridview has also a column of DateTime, the strange thing is when applying the defaultcellstyle.Format as 'N2', the columns of decimal or float are displayed well as I expect, but the column of DateTime doesn't show strings of DateTime, instead it shows values of N2, but I added a CellFormatting event handler for my datagridview to handle this and it is OK, I highly appreciate you if you have any other better solution for this, and some explanation for why this happens to column of DateTime if possible. Thanks again!
Daniele Rota Nodari 17-Jan-13 2:54am    
Hi.
I'm glad I was helpful. :)
About your other issue, have you assigned format "N2" to the DateTime column? This should be the only cause of the problem. Keep in mind that every type works with different format strings: "N2" is valid for some numerical types (like float) but not valid for others. For DateTime you have to use one of the default formats (e.g. "D") or custom formats (e.g. "dd-MM-yyyy").

If you still have problems, I suggest you to post another question on the forums in order to receive the best response possible and to share the solution with anyone else.

Regards, Daniele.
supernorb 28-Jan-13 8:58am    
Oh, I might do wrong not like your instruction, instead of assigning 'N2' to DefaultCellStyle.Format of the numeric column, I assigned it to DefaultCellStyle.Format of the DataGridView. That's why my next issue occured.

Thank you for the helpful comment! I haven't any issue anymore now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900