Tuesday, May 19, 2015

Rounding Prices to Specific Digits

The other day I was tasked with changing some prices on some things in our database. Easy enough, but the trick was having them all end in 0, 5 or 9. That was where it because a little bit more difficult. However, using the modulo operator made it a relatively easy operation. Check out my code below to see how I approached this challenge.

CASE round(curprice*@pricemult,2) % 1 % .1
 WHEN .01 THEN   .00
 WHEN .02 THEN   .00
 WHEN .03 THEN   .05
 WHEN .04 THEN   .05
 WHEN .06 THEN   .05
 WHEN .07 THEN   .05
 WHEN .08 THEN   .09
 ELSE round(curprice*@pricemult,2) % 1 % .1
END + (round(curprice*@pricemult,2) - (round(curprice*@pricemult,2) % 1 % .1)))[New Price]


Read more about modulo.

Feel free to leave your own thoughts/comments below.

No comments:

Post a Comment