How can I multiply two decimals and round the result down to 2 decimal places?
For example if the equation is 41.75 x 0.1 the result will be 4.175. If I do this in c# with decimals it will automatically round up to 4.18. I would like to round down to 4.17.
I tried using Math.Floor but it just rounds down to 4.00. Here is an example:
Math.Floor (41.75 * 0.1);
The
Math.Round(...)function has an Enum to tell it what rounding strategy to use. Unfortunately the two defined won’t exactly fit your situation.The two Midpoint Rounding modes are:
What you want to use is
Floorwith some multiplication.The
outputvariable should have 4.17 in it now.In fact you can also write a function to take a variable length as well: