MuMu88
Aged Yak Warrior
549 Posts |
Posted - 2013-05-20 : 18:02:03
|
Here is a SQL function that performs Java HALF_UP and HALF_DOWN rounding; the usage of the function is self-explanatory.Pass a numeric value and 'UP' as second parameter if you want to perform HALF_UP or 'DOWN' as second parameter if you want to perform HALF_DOWN.[CODE]IF OBJECT_ID('dbo.Java_HALF_UP_DOWN') IS NOT NULL DROP FUNCTION dbo.Java_HALF_UP_DOWN;GOCREATE FUNCTION dbo.Java_HALF_UP_DOWN( @nval AS NUMERIC(12,5), @type AS VARCHAR(8))RETURNS NUMERIC(12,5)AS BEGIN DECLARE @cval AS NUMERIC(12, 5) DECLARE @sval AS NUMERIC(2,1) DECLARE @tval AS INT SET @sval = CASE WHEN @nval < 0 THEN -1 ELSE 1 END SET @cval = ABS(@nval) - FLOOR(ABS(@nval)) RETURN (CASE WHEN (@type = 'DOWN') and @cval <= 0.5 THEN FLOOR(ABS(@nval)) WHEN (@type = 'UP') and @cval < 0.5 THEN FLOOR(ABS(@nval)) ELSE CEILING(ABS(@nval)) END) * @sval;ENDGOUSAGE: SELECT 88887.50001 as val, dbo.Java_HALF_UP_DOWN(88887.50001, 'UP') as Java_Half_UPUNION ALLSELECT 88887.49999 as val, dbo.Java_HALF_UP_DOWN(88887.49999, 'UP')UNION ALLSELECT 88887.50000 as val, dbo.Java_HALF_UP_DOWN(88887.50000, 'UP')UNION ALLSELECT -88887.50000 as val, dbo.Java_HALF_UP_DOWN(-88887.50000, 'UP')UNION ALLSELECT 5.5 as val, dbo.Java_HALF_UP_DOWN(5.5, 'UP')UNION ALLSELECT 2.5 as val, dbo.Java_HALF_UP_DOWN(2.5, 'UP')UNION ALLSELECT 1.6 as val, dbo.Java_HALF_UP_DOWN(1.6, 'UP')UNION ALLSELECT 1.1 as val, dbo.Java_HALF_UP_DOWN(1.1, 'UP')UNION ALLSELECT 1.0 as val, dbo.Java_HALF_UP_DOWN(1.0, 'UP')UNION ALLSELECT -1.0 as val, dbo.Java_HALF_UP_DOWN(-1.0, 'UP')UNION ALLSELECT -1.1 as val, dbo.Java_HALF_UP_DOWN(-1.1, 'UP')UNION ALLSELECT -1.6 as val, dbo.Java_HALF_UP_DOWN(-1.6, 'UP')UNION ALLSELECT -2.5 as val, dbo.Java_HALF_UP_DOWN(-2.5, 'UP')UNION ALLSELECT -5.5 as val, dbo.Java_HALF_UP_DOWN(-5.5, 'UP');RESULTS:val JAVA_Half_UP88887.50001 88888.0000088887.49999 88887.0000088887.50000 88888.00000-88887.50000 -88888.000005.50000 6.000002.50000 3.000001.60000 2.000001.10000 1.000001.00000 1.00000-1.00000 -1.00000-1.10000 -1.00000-1.60000 -2.00000-2.50000 -3.00000-5.50000 -6.00000SELECT 88887.50001 as val, dbo.Java_HALF_UP_DOWN(88887.50001, 'DOWN') as Java_Half_DOWNUNION ALLSELECT 88887.49999 as val, dbo.Java_HALF_UP_DOWN(88887.49999, 'DOWN')UNION ALLSELECT 88887.50000 as val, dbo.Java_HALF_UP_DOWN(88887.50000, 'DOWN')UNION ALLSELECT -88887.50000 as val, dbo.Java_HALF_UP_DOWN(-88887.50000, 'DOWN')UNION ALLSELECT -88887.5881 as val, dbo.Java_HALF_UP_DOWN(-88887.5881, 'DOWN')UNION ALLSELECT 5.5 as val, dbo.Java_HALF_UP_DOWN(5.5, 'DOWN')UNION ALLSELECT 2.5 as val, dbo.Java_HALF_UP_DOWN(2.5, 'DOWN')UNION ALLSELECT 1.6 as val, dbo.Java_HALF_UP_DOWN(1.6, 'DOWN')UNION ALLSELECT 1.1 as val, dbo.Java_HALF_UP_DOWN(1.1, 'DOWN')UNION ALLSELECT 1.0 as val, dbo.Java_HALF_UP_DOWN(1.0, 'DOWN')UNION ALLSELECT -1.0 as val, dbo.Java_HALF_UP_DOWN(-1.0, 'DOWN')UNION ALLSELECT -1.1 as val, dbo.Java_HALF_UP_DOWN(-1.1, 'DOWN')UNION ALLSELECT -1.6 as val, dbo.Java_HALF_UP_DOWN(-1.6, 'DOWN')UNION ALLSELECT -2.5 as val, dbo.Java_HALF_UP_DOWN(-2.5, 'DOWN')UNION ALLSELECT -5.5 as val, dbo.Java_HALF_UP_DOWN(-5.5, 'DOWN');RESULTS:val JAVA_Half_DOWN88887.50001 88888.0000088887.49999 88887.0000088887.50000 88887.00000-88887.50000 -88887.00000-88887.58810 -88888.000005.50000 5.000002.50000 2.000001.60000 2.000001.10000 1.000001.00000 1.00000-1.00000 -1.00000-1.10000 -1.00000-1.60000 -2.00000-2.50000 -2.00000-5.50000 -5.00000[/CODE]Refer to the following site for more informationhttp://docs.oracle.com/javase/6/docs/api/java/math/RoundingMode.htmlEDITED: Much simpler solution without string manipulations |
|