I am trying to use Decimal.Round to round a decimal value to 0 decimal precision. It's giving me different results for different decimal values. Decimal.Round( 41.5, 0) = 42It may be due to the fact that Decimal.Round function with 0 precision is rounding the decimal to nearest even number if the fractional component of the decimal is 0.5. Does anyone know what I am doing anything wrong here and a solution for this?
asked
Nitin Chhabra |

Graphite’s Decimal.Round system function uses Math.Round function of .net framework that has default rounding to nearest even (banker’s rounding). If you are looking to round the decimal (D) with fractional component >= 0.5 to D + 1 and <0.5 to D -1, then here is the possible solution: Create a behavior with the following logic – - Use Decimal.Floor to get the floor value of decimal (D)
- Subtract the decimal (D) from the floor value to get the fractional component
- Use a decision shape to check the fractional component is >=0.5 a. If >=0.5, use Decimal.Ceiling and assign it to return value b. If <0.5, use Decimal.Floor and assign it to return value
You can run this behavior from places where you expect to see the above results. Hope this helps.
answered
Nitin Chhabra |