简体   繁体   中英

how to round decimal values to number in sql server

I want to update decimal values using 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.

You would use the column name discount_perc instead of 0.4 in the following examples:


You can use cast() like so:

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

returns: 40%

rextester demo: http://rextester.com/PSPVY57957


In SQL Server 2012+ you can use format() :

select format(0.4,'0%')

returns: 40%

But format() can be slower, take a look here: format() is nice and all, but… - Aaron Bertrand

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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