I already have several ways to solve this, but I am interested in whether there is a better solution to this problem. Please only respond with a pure numeric algorithm. String manipulation is not acceptable. I am looking for an elegant and efficient solution.
Given a currency value (ie $251.03), split the value into two half and round to two decimal places. The key is that the first half should round up and the second should round down. So the outcome in this scenario should be $125.52 and $125.51.
Divide by two, round to 2 d.p. (in C# this is
decimal.Round(value, 2)), subtract the rounded value from the original, and sort them using an if. Your library may support control over the rounding which can save you the if – with C# you can do this using the 3-parameter overload ofdecimal.Round.