[英]how to round decimal values to number in sql server
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.