简体   繁体   English

如何在SQL Server中将十进制值舍入为数字

[英]how to round decimal values to number in sql server

I want to update decimal values using CAST FUNCTION 我想使用CAST FUNCTION更新十进制值

Example values: 值示例:

Discount_Value

Result like this: 结果如下:

Discount_Result

That type of formatting should probably be done in your application layer rather than in SQL Server. 这类格式化可能应该在您的应用程序层而不是在SQL Server中完成。

You would use the column name discount_perc instead of 0.4 in the following examples: 在以下示例中,将使用列名discount_perc代替0.4


You can use cast() like so: 您可以像这样使用cast()

select cast(cast(0.4*100 as int) as varchar(12))+'%'

returns: 40% 回报率: 40%

rextester demo: http://rextester.com/PSPVY57957 extrester演示: http ://rextester.com/PSPVY57957


In SQL Server 2012+ you can use format() : 在SQL Server 2012+中,您可以使用format()

select format(0.4,'0%')

returns: 40% 回报率: 40%

But format() can be slower, take a look here: format() is nice and all, but… - Aaron Bertrand 但是format()速度可能会变慢,请看这里: format()很好,而且所有...-Aaron Bertrand

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

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