Monday, August 28, 2006

Round Up Or Down To Nearest Percentage Value By Using FLOOR And CEILING

Let's say you have a value of 13.33 and you want to round this up and down to the nearest .25 in other words for 13.33 you want to display 13.25 and 13.50.How do you do that? It's pretty easy you do FLOOR(Value *4)/4 and CEILING(Value *4)/4
Below are 3 example, 1 for 0.25, 1 for 0.50 and 1 for 0.33


--0.25
DECLARE @Value DECIMAL(10,2)

SET @Value = 13.33

SELECT FLOOR(@Value * 4) / 4.0,CEILING(@Value * 4) / 4.0
GO


--0.50
DECLARE @Value DECIMAL(10,2)

SET @Value = 13.33

SELECT FLOOR(@Value * 2) / 2.0,CEILING(@Value * 2) / 2.0
GO


--0.33
DECLARE @Value DECIMAL(10,2)

SET @Value = 13.36

SELECT FLOOR(@Value * 3) / 3.0,CEILING(@Value * 3) / 3.

1 comment:

Anonymous said...

hi did you come up divisor and multiplicator to your FLOOR? esp. in 0.33

nice post btw,

thanks