Click here to Skip to main content
11,639,463 members (70,484 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
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 5-Jan-13 0:17am
supernorb2.6K

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
Column1.DefaultCellStyle.Format = "N2"; // number with two digits after point

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

Regards,
Daniele.
  Permalink  
Comments
supernorb at 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 at 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 at 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)

  Print Answers RSS
0 Suvendu Shekhar Giri 320
1 Sergey Alexandrovich Kryukov 283
2 Richard Deeming 200
3 CPallini 187
4 PIEBALDconsult 175
0 OriginalGriff 9,081
1 Sergey Alexandrovich Kryukov 8,812
2 Mika Wendelius 7,027
3 Suvendu Shekhar Giri 2,600
4 F-ES Sitecore 2,548


Advertise | Privacy | Mobile
Web01 | 2.8.150728.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100