T-SQL Round Up function Revisited
In October Last year I posted a function to round up a number. I have had to adapt that function to cater for varied decimal places. Please email me any suggestions or alterations.
CREATE FUNCTION [dbo].[udfRoundUp]
(
@Num1 FLOAT,
@NumberOfDecimalPlaces INT = 0
)
RETURNS FLOAT
AS
BEGIN
DECLARE @Temp FLOAT
SET @Temp = 0
IF @Num1 - CAST(ROUND(@Num1, @NumberOfDecimalPlaces) AS FLOAT) > 0
BEGIN
/* When rounding we cannot just add 1 */
/* Calculate the round up value */
SET @Temp = POWER(10, (@NumberOfDecimalPlaces * -1))
END
RETURN ROUND(@Num1, @NumberOfDecimalPlaces) + @Temp
END
CREATE FUNCTION [dbo].[udfRoundUp]
(
@Num1 FLOAT,
@NumberOfDecimalPlaces INT = 0
)
RETURNS FLOAT
AS
BEGIN
DECLARE @Temp FLOAT
SET @Temp = 0
IF @Num1 - CAST(ROUND(@Num1, @NumberOfDecimalPlaces) AS FLOAT) > 0
BEGIN
/* When rounding we cannot just add 1 */
/* Calculate the round up value */
SET @Temp = POWER(10, (@NumberOfDecimalPlaces * -1))
END
RETURN ROUND(@Num1, @NumberOfDecimalPlaces) + @Temp
END