Accroding to Math.Round
, Notes to Callers section
Because of the loss of precision that can result from representing
decimal values as floating-point numbers or performing arithmetic
operations on floating-point values, in some cases the Round(Double, Int32, MidpointRounding)
method may not appear to round midpoint
values as specified by the mode
parameter. This is illustrated in the
following example, where 2.135 is rounded to 2.13 instead of 2.14.
This sounds like your exact case, due the loss of precision 19.185
is rounded to 19.18
instead of 19.19
. You can display values using the G17
format specifier to see all significant digits of precision
Console.WriteLine(1.235.ToString("G17"));
Console.WriteLine(19.185.ToString("G17"));
The output will be something like that
1.2350000000000001
19.184999999999999
As possible workaround, you can use decimal
values with better precision
var r = Math.Round(1.235m, 2, MidpointRounding.AwayFromZero);
var t = Math.Round(19.185m, 2, MidpointRounding.AwayFromZero);
The result will be expected