Hello,
I have a weird problem with rouding in the Grid and C#.
I have following number = 260.7045.
I have a mask in the column that states only 3 decimal places:
{LOC}-nn,nnn,nnn,nnn.nnn
The grid will thus display 260.705
This is the sort of result that one would expect. However, i also have a report that has to display this number correctly. In this case, because i need to do further calculation with this number, i have to round the number.
Much to my surprise the rounded number becomes 206.704 in c#.
I figured out that this is because c# uses bankers rounding, so i figured that i needed to use an override of Math.Round:
Math.Round(260.7045, 3, MidpointRounding.AwayFromZero) == 260.705
However, when i try to the following calculation:
Math.Round(141.855, 2, MidpointRounding.AwayFromZero) == 141.85
==> Why does the function not return 141.86, when you try this in the grid it will display 141.86
I realize this is a C# problem, however... my question is, how does the mask work, which function does infragistics use and can we use it too in our app, so we have a consistent way of calculation.
Brian,
I have a problem in the round up. I posted my problem in the below mentioned post :
http://forums.infragistics.com/forums/p/60879/311184.aspx#311184
Guide me for the problem.
Hi Brian,
Thank you for the quick reply.
Converting to decimal returned the sort of rounding we needed.
Best Regards,
Peter
I forgot to mention that Math.Round provides an overload that takes a value of type System.Decimal, and for that data type the results seem to be more in line with what you would expect.
Calling Math.Round on the value 141.855 gives different results than 125.855. I investigated this and it seems that there is a threshold for the integral component of the number, namely 128, at which the rounding behavior changes. 127.855 rounds to 127.86 (as expected), but 128.855 rounds to 128.85. This seems to be a nuance of floating point rounding; it might have something to do with the bit size of the integral component as relative to the size of the fractional component.
EditorWithMask multiplies the fractional component of the number by 10^n, where n is the number of digits in the fractional component. We do this because the FractionPart class knows the number as an integral number, i.e., using 141.855 as an example, the value for the FractionPart section is eight hundred fifty-five. A consequence of rounding it to an integer is that it masks the Math library's rounding quirkiness.