Round to 25, 50, 75, 100

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

    I would use something like this:

    float RoundNearestCents(float price)
        {
        price*=(100/25.0); // now fractions are going away
        if (price-floor(price)>=0.5) price++; // round up if fraction above 0.5
        return floor(price)*(25.0/100.0); // cut of the fraction and restore original range
        }
    

提交回复
热议问题