4

I have this number 0.581183781439, I need to round the number 0.582

I was trying with

SELECT ROUND (0.581183781439, 3)
-------------------------------
0.581000000000

Is it useful ROUND function for this?

4

4 回答 4

11
DECLARE @Test TABLE (Col NUMERIC(38,12));

INSERT  @Test (Col)
SELECT 0.581183781439 
UNION ALL
SELECT 0.5815
UNION ALL
SELECT 0.581883781439
UNION ALL
SELECT -0.581883781439;

SELECT Col AS [Col],
    ROUND(Col, 3) AS StandardRounding_3decimals,
    ROUND(Col, 3, 1) AS Truncation_3decimals,
    FLOOR(Col*1000)/1000 AS RoundDown_3decimals,
    CEILING(Col*1000)/1000  AS RoundUp_3decimals 
FROM @Test;

Results:

Col             StandardRounding_3decimals Truncation_3decimals RoundDown_3decimals RoundUp_3decimals
--------------- -------------------------- -------------------- ------------------- -----------------
0.581183781439  0.581000000000             0.581000000000       0.581000            0.582000
0.581500000000  0.582000000000             0.581000000000       0.581000            0.582000
0.581883781439  0.582000000000             0.581000000000       0.581000            0.582000
-0.581883781439 -0.582000000000            -0.581000000000      -0.582000           -0.581000
于 2012-12-27T19:12:14.527 回答
7

Maybe you need this?

SELECT ROUND(0.581183781439, 3,1) + .001

but correct rounding is 0.581.

于 2012-12-27T18:10:52.070 回答
5

We can write a T-SQL function to round up for an arbitrary number of decimal places.

CREATE FUNCTION RoundUp (@value float, @places int) RETURNS float
AS
BEGIN
    RETURN SELECT CEILING(@value * POWER(10, @places)) / POWER(10, @places)
END
GO

SELECT Result = dbo.RoundUp(0.581183781439, 3)

Result

0.582

于 2014-09-23T09:41:10.153 回答
0

Adding a half value and using normal round will get the correct result.

SELECT ROUND (0.581183781439 + .0005, 3)

Bonus: Subtract the half value instead, and it will round down.

EDIT: Actually, this has the same flaw as an answer above when the value is zeros after the 3rd decimal.

于 2020-04-05T00:03:34.660 回答