Practical .NET · Lesson 4

Money

Round money once, at the end, with an explicit rule.

How it works

Splitting 100 between three people gives 33.333..., which is not a price. Math.Round with two decimals makes 33.33. Three of those add up to 99.99, so one cent is missing. The last share takes the remainder, which keeps the sum exactly 100. MidpointRounding.AwayFromZero rounds 2.345 up to 2.35, the way most receipts do.

A working example

var bill = 100m;
var people = 3;
var share = Math.Round(bill / people, 2);
var last = bill - share * (people - 1);

Console.WriteLine($"{share} {share} {last}");
Console.WriteLine(share * (people - 1) + last);
Console.WriteLine(Math.Round(2.345m, 2, MidpointRounding.AwayFromZero));

Expected output

33.33 33.33 33.34
100.00
2.35
Try this
Give the rounding difference to one share.