Skip to content

Instantly share code, notes, and snippets.

@mshock
Last active October 11, 2015 21:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mshock/3920316 to your computer and use it in GitHub Desktop.
Save mshock/3920316 to your computer and use it in GitHub Desktop.
sql function - round float to sig fig
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);
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
@mshock
Copy link
Author

mshock commented Aug 19, 2013

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment