Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 rounding decimal values by 0.25

Author  Topic 

afroz
Starting Member

6 Posts

Posted - 2011-05-24 : 01:36:19
I'm Trying to round up decimal values by 0.25 Can any body Help me on this..please i'm badly stuck..

For example,

For 2.40 It shold show 2.50

2.35 to 2.50

2.15 To 2.25

2.60 To 2.75


afrozz..

raghuveer125
Constraint Violating Yak Guru

285 Posts

Posted - 2011-05-24 : 02:54:42
MAY BE THIS WILL HELP YOU TRY
Declare @a numeric(10,2)
Set @a=2.76--change this values and check
SELECT
CASE WHEN @a<CEILING(@a) THEN
CASE WHEN 1-(CEILING(@a)-@a)<.25 THEN @a-(1-(CEILING(@a)-@a))+.25
ELSE
CASE WHEN 1-(CEILING(@a)-@a)<.50 THEN @a-(1-(CEILING(@a)-@a))+.50
ELSE
CASE WHEN 1-(CEILING(@a)-@a)<.75 THEN @a-(1-(CEILING(@a)-@a))+.75
ELSE CEILING(@a)
END
END
END
END

In Love... With Me!
Go to Top of Page

mhorseman
Starting Member

44 Posts

Posted - 2011-05-24 : 04:24:11
A bit less typing ...

declare @amt numeric(10,2)
set @amt = 2.76

select floor(@amt) + ceiling(@amt % 1 * 4) / 4

Mark
Go to Top of Page

afroz
Starting Member

6 Posts

Posted - 2011-05-24 : 07:54:43
Thanks mhorseman,

you really saved my ****,i was looking for this .Also thanks to raghuveer125 for replying..

Once again thanks guys..


afrozz..
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-05-24 : 13:47:30
Here is one more for fun:
DECLARE @Foo TABLE (Val DECIMAL(18,2))

INSERT @Foo (Val)
VALUES (2.35),(2.15),(2.60)

SELECT CEILING(Val / 0.25) * 0.25
FROM @Foo
Go to Top of Page
   

- Advertisement -