Rounding functions in SQL Server

Source: Internet
Author: User
Tags integer numeric

Problem

Recently I tried to use rounded values in my application, and my users were divided about the computational problems in the report application. All of the code is in T-SQL, but I think the report problem is closely related to the data type and the rounding or up rule. Do you have any suggestions for this? I'd like to see some examples with different coding options.

Expert answers

If you do not understand the basic data types and rounding functions, then you may misunderstand rounding. Because of differences in data types (such as integer,float, Decimal, and so on), rounding values may vary. In addition, the resulting value may be different because of the difference between the SQL Server rounding functions used in the calculation (ROUND (), CEILING (), FLOOR ()). Therefore, it is important to find the user's requirements for rounding and then translate those requirements into the appropriate T-SQL commands.

Let's start with a defined perspective:

L ROUND () – Rounded a positive or negative number, resulting in a value of a certain length.

L CEILING ()-Returns the smallest integer so that the integer is greater than or equal to the numeric operation of the specified number.

L FLOOR ()-Returns the largest integer so that the integer is less than or equal to the numeric operation of the specified number.

Let's take a look at the results of functions with different data types.

ROUND (), CEILING () and floor () examples
Example Value
In this example, you can see that in the case of a positive integer, these three rounding functions return the same value.
DECLARE @value int SET @value = 6 Select ROUND (@value, 1) Select CEILING (@value) Select FLOOR (@value) 6 6 6
In the second example, even in the case of a negative integer, the three rounding functions return the same value.
DECLARE @value int SET @value = -11 Select ROUND (@value, 1) Select CEILING (@value) Select FLOOR (@value) -11-11-11
Rounding is not possible to prove the whole idea of integers. Let's take a look at some of the other data types.
DECLARE @value int SET @value = -11.5 Select ROUND (@value, 2) Select CEILING (@value) Select FLOOR (@value) -11-11-11
In our example, a rounding function with a decimal data type and a different length parameter (such as 1,2 or 3) produces a different end value. When rounding this value and the length parameter is 1 o'clock, 5 of the second digit after the decimal point is meaningful. In addition, in the Decimal data type, the ceiling and floor functions also consider decimal places because different values are obtained.
DECLARE @value Decimal (10,2) SET @value = 11.05 Select ROUND (@value, 1) Select ROUND (@value, 2) Select ROUND (@value, 3) SE Lect CEILING (@value) SELECT FLOOR (@value) 11.10 11.05 11.05 12 11
As in the example above, 6 of the second digit after the decimal point is meaningful based on the different length parameters.
DECLARE @value Decimal (10,2) SET @value = -14.46 Select ROUND (@value, 1) Select ROUND (@value, 2) Select ROUND (@value, 3) S Elect CEILING (@value) SELECT FLOOR (@value) -14.50-14.46-14.46-14-15
This example also helps to describe the destruction of rounding values. This example also proves that the ceiling and FLOOR functions are rounded to the nearest function.
DECLARE @value Decimal (10,10) SET @value =. 5432167890 Select ROUND (@value, 1) Select ROUND (@value, 2) Select ROUND (@value , 3 Select ROUND (@value, 4) Select ROUND (@value, 5) Select ROUND (@value, 6) Select ROUND (@value, 7) Select ROUND (@value, 8) Select ROUND (@value, 9) Select ROUND (@value) Select CEILING (@value) Select FLOOR (@value) 0.5000000000 0.5400000000 0.5430000000 0.5432000000 0.5432200000 0.5432170000 0.5432168000 0.5432167900 0.5432167890 0.5432167890 1 0
In the final example, you can see that the same type of behavior is used in floating-point data types as in the case of the decimal point above. In addition, the ceiling and floor functions are rounded to the nearest function.
DECLARE @value Float (a) SET @value =. 1234567890 Select ROUND (@value, 1) Select ROUND (@value, 2) Select ROUND (@value, 3) Select ROUND (@value, 4) Select ROUND (@value, 5) Select ROUND (@value, 6) Select ROUND (@value, 7) Select ROUND (@value, 8) SE Lect ROUND (@value, 9) Select ROUND (@value,) Select CEILING (@value) Select FLOOR (@value) 0.1 0.12 0.123 0.1235 0.12346 0.123457 0.1234568 0.12345679 0.123456791 0.123456791 1-0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.