I'd like to know what the C# equivalent of the following VB6 code is that will generate the exact same results:
Public Function Moo(o1 As Currency, o2 As Double) As Currency
Moo = Round((o1 * o2) / 100, 2)
End Function
Before you answer with Math.Round(o1 * (decimal)o2 / 100, 2)
, consider the following test case:
o1 = 1450 o2 = 1.15
The VB6 function returns 16.67 and that C# code returns 16.68.
Next option: (decimal)Math.Round((double)o1 * o2 / 100, 2)
That generates the same result but then there's this test case:
o1 = 1570 o2 = 1.15
Now, the VB code generates 18.06 but the C# generates 18.05.
I understand the reasoning behind why this is happening and why one should never use double
for calculations that require accuracy and that the VB6 code is doing things it shouldn't. Consider this an academic exercise. Essentially, is it possible to get the following test to pass:
[Theory]
[InlineData(1450, 1.15, 16.67)]
[InlineData(1570, 1.15, 18.06)]
public void MooTest(decimal o1, double o2, decimal expectedValue) {
var value = {MAGICAL METHOD CODE HERE};
Assert.Equal(expectedValue, value);
}