简体   繁体   English

SQL-在小数点后第六位截断

[英]SQL - Cut off at 6th decimal place

So, I have a value 123.123456789, and I'm trying to return 123.123456. 因此,我有一个值123.123456789,并且我试图返回123.123456。 The query I tried below rounds the 6th decimal digit to the next number. 我在下面尝试的查询将第6个十进制数字舍入到下一个数字。 Please help to return 123.123456 ONLY. 请帮助仅返回123.123456。 (No rounding!) (不四舍五入!)

select cast(cast(123.123456789 as DECIMAL(9,6)) as float)

Use ROUND . 使用ROUND The first param is your number, second is the precision, and the third is a number that indicates if the rounding should truncate (0 rounds, any other number truncates). 第一个参数是您的数字,第二个参数是精度,第三个参数是指示舍入是否应该截断的数字(0个回合,其他任何数字都将截断)。

select cast(cast(round(123.123456789,6,1) as DECIMAL(9,6)) as float)

select round(numeric_expression, length [, function])

Arguments 争论

numeric_expression Is an expression of the exact numeric or approximate numeric data type category, except for the bit data type. numeric_expression是精确数字或近似数字数据类型类别的表达式,位数据类型除外。

length Is the precision to which numeric_expression is to be rounded. length是numeric_expression要舍入的精度。 length must be an expression of type tinyint, smallint, or int. length必须是tinyint,smallint或int类型的表达式。 When length is a positive number, numeric_expression is rounded to the number of decimal positions specified by length. 当length为正数时,numeric_expression将舍入为length指定的小数位数。 When length is a negative number, numeric_expression is rounded on the left side of the decimal point, as specified by length. 当length为负数时,numeric_expression在小数点的左侧舍入,由length指定。

function Is the type of operation to perform. 功能是要执行的操作的类型。 function must be tinyint, smallint, or int. 函数必须是tinyint,smallint或int。 When function is omitted or has a value of 0 (default), numeric_expression is rounded. 如果省略函数或值为0(默认值),则将数字表达式四舍五入。 When a value other than 0 is specified, numeric_expression is truncated. 当指定非0的值时,numeric_expression被截断。

as explained here: https://docs.microsoft.com/en-us/sql/t-sql/functions/round-transact-sql?view=sql-server-2017 如此处所述: https : //docs.microsoft.com/zh-cn/sql/t-sql/functions/round-transact-sql?view=sql-server-2017

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM