Click here to Skip to main content
12,943,742 members (56,778 online)
Rate this:
Please Sign up or sign in to 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!
Posted 5-Jan-13 0:17am

1 solution

Rate this: bad
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.

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
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)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,643
CHill60 2,970
Maciej Los 2,348
Jochen Arndt 1,900
ppolymorphe 1,765

Advertise | Privacy | Mobile
Web01 | 2.8.170518.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2017
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