Last active
October 11, 2015 21:08
-
-
Save mshock/3920316 to your computer and use it in GitHub Desktop.
sql function - round float to sig fig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GO | |
CREATE FUNCTION RoundSigFig(@Number float, @Figures int) | |
RETURNS float | |
AS | |
BEGIN | |
DECLARE @Answer float; | |
SET @Answer = ( | |
SELECT | |
CASE WHEN intPower IS NULL THEN 0 | |
ELSE FLOOR(fltNumber * POWER(CAST(10 AS float), intPower) + 0.5) | |
* POWER(CAST(10 AS float), -intPower) | |
END AS ans | |
FROM ( | |
SELECT | |
@Number AS fltNumber, | |
CASE WHEN @Number > 0 | |
THEN -((CEILING(LOG10(@Number)) - @Figures)) | |
WHEN @Number < 0 | |
THEN -((FLOOR(LOG10(@Number)) - @Figures)) | |
ELSE NULL END AS intPower | |
) t | |
); | |
RETURN @Answer; | |
END; | |
GO | |
declare @SigFigRight float; | |
select dbo.RoundSigFig(1806.6600958604099,15); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create function sfround(@number float, @sf int) returns float as | |
begin | |
declare @r float | |
select @r = case when @number = 0 then 0 else round(@number ,@sf -1-floor(log10(abs(@number )))) end | |
return (@r) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've since discovered that this function throws an error for negative values. Now using the second function.
Source:
http://stackoverflow.com/questions/1920933/round-to-n-significant-figures-in-sql