Round decimal to the nearest even cent

How do you get to the nearest even cent ?. So, for the following decimal places, I want to round and display the amount in dollars:

1230.3092 => $1230.30
221.9672 => $221.96
12345.6808 => 12345.68

I tried this

Math.Round(value, 2, MidpointRounding.ToEven)
decimal.Round(value, 2, MidpointRounding.ToEven)

but for some reason it is rounded.

+4
source share
2 answers

Do you want to:

(0.02m / 1.00m) * decimal.Round(value * (1.00m / 0.02m))

value * (1.00m / 0.02m)- the amount of money in units of "2 cents." Round in this space to an integer, and then convert back to dollars.

MidpointRoundingshould be used to determine how you want to 100.01mround, for example , since it is halfway between 100.00mand 100.02m.

+4
source

, , . , - , , , ( ).

+1

Source: https://habr.com/ru/post/1616421/


All Articles