Round to n Significant Figures in SQL

前端 未结 4 1534
-上瘾入骨i
-上瘾入骨i 2021-02-18 14:38

I would like to be able to round a number to n significant figures in SQL. So:

123.456 rounded to 2sf would give 120
0.00123 rounded to 2sf would give 0.0012
         


        
4条回答
  •  执笔经年
    2021-02-18 14:41

    Adapted the most popular answer by Brann to MySQL for those who come looking like me.

    CREATE FUNCTION `sfround`(num FLOAT, sf INT) # creates the function
    RETURNS float # defines output type
    DETERMINISTIC # given input, will return same output
    
    BEGIN
    
        DECLARE r FLOAT;  # make a variable called r, defined as a float
    
        IF( num IS NULL OR num = 0) THEN # ensure the number exists, and isn't 0
            SET r = num; # if it is; leave alone
    
        ELSE
            SET r = ROUND(num, sf - 1 - FLOOR(LOG10(ABS(num))));
        /* see below*/
        END IF;
    
        RETURN (r);
    
    END
    

    /* Felt too long to put in comment */

    ROUND(num, sf - 1 - FLOOR(LOG10(ABS(num))))

    • The part that does the work - uses ROUND function on the number as normal, but the length to be rounded to is calculated
    • ABS ensures positive
    • LOG10 gets the number of digits greater than 0 in the number
    • FLOOR gets the largest integer smaller than the resultant number
    • So always rounds down and gives an integer
    • sf - 1 - FLOOR(...) gives a negative number
    • works because ROUND(num, -ve num) rounds to the left of the decimal point

    • For just a one off, ROUND(123.456, -1) and ROUND(0.00123,4) return the requested answers ((120, 0.0012)

提交回复
热议问题