简体   繁体   English

如何在SQL Server中舍入十进制值

[英]how to round the decimal value in sql server

I want to round the value from 1572.750000 to 1572.75 我想将值从1572.750000舍入到1572.75

SELECT ROUND(1572.750000, 2)

I tried above format, but it was not working. 我尝试了上述格式,但无法正常工作。 Please any one help me out. 请任何人帮我。

I think the simplest way is to convert to decimal : 我认为最简单的方法是将其转换为decimal

select cast(1572.750000 as decimal(10, 2))

If you want a string output, you can use the str() function (documented here ). 如果要输出字符串,可以使用str()函数( 在此处记录 )。

The confusion occurs because round() changes the value , but it does not change the type . 发生混乱的原因是round()更改 ,但不会更改type Hence, whatever application is using the value can format it as it wants. 因此,无论使用哪个值的应用程序都可以根据需要设置其格式。 When the application knows that the value has only two decimal places (with decimal(10, 2) ) or the code converts directly to a string, then this is not an issue. 当应用程序知道该值仅具有两个小数位(带有decimal(10, 2) )或代码直接转换为字符串时,这不是问题。

你可以用这个

SELECT cast(1572.750000 as decimal(15,2))

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

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