Round to 25, 50, 75, 100

后端 未结 5 2007
说谎
说谎 2021-01-28 03:49

I\'m not a Math person so I\'m having a hard time to come up with a calculation to round the decimals to 25, 50, 75 and 100. And this will not be the typical round off because t

5条回答
  •  梦谈多话
    2021-01-28 04:17

    This is one way:

    public decimal RoundNearestCents(decimal price)
    {
        decimal srp = price * 100;
        decimal m = srp % 25;
        srp = srp - m + (m > 0 ? 25 : 0);
        return srp / 100;
    }
    

提交回复
热议问题