Round to 25, 50, 75, 100

后端 未结 5 1948
说谎
说谎 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:12

    My code may not be the best out there, but it will work. In your function create a float and an int like so.

    public float RoundNearestCents(String price)
    {
        float srp = float.Parse(price);
        int srp1 = Int32.Parse(price);
        if((srp-srp1)>=0.5) 
            srp1++;
        else 
            return srp1;
        return srp1;
    }
    

    The int would truncate out the decimal part, which is like flooring the price.

提交回复
热议问题