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.
For Example Decimal.Round( 40.5, 0 ) = 40 and Decimal.Round( 41.5, 0) = 42
It 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 30 Sep '20, 11:33

Nitin%20Chhabra's gravatar image

Nitin Chhabra
accept rate: 31%

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 –

  1. Use Decimal.Floor to get the floor value of decimal (D)
  2. Subtract the decimal (D) from the floor value to get the fractional component
  3. 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 30 Sep '20, 11:35

Nitin%20Chhabra's gravatar image

Nitin Chhabra
accept rate: 31%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: 30 Sep '20, 11:33

Seen: 744 times

Last updated: 30 Sep '20, 11:35

Related questions

Powered By:   Graphite