SQL 舍入并显示到 2 个小数位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28408400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:16:06  来源:igfitidea点击:

Round And Show To 2 Decimal Places?

sqlsql-server

提问by Rejwanul Reja

I have a small question to ask. How to round a numeric field upto 2 decimal places, and also show it with 2 decimal places only

我有一个小问题要问。如何将数字字段舍入到 2 个小数位,并仅显示 2 个小数位

For example the following would return 255.88000000000

例如,以下将返回 255.88000000000

select round(255.87908765444,2)

How to get 255.88 only?

如何仅获得 255.88?

Pleas help.

请帮忙。

Thanks,

谢谢,

回答by t-clausen.dk

All you need is:

所有你需要的是:

CAST(255.87908765444 as decimal(18,2)). 

When you convert data types in which the target data type has fewer decimal places than the source data type, the value is rounded. From microsoft

当您转换目标数据类型的小数位数少于源数据类型的数据类型时,该值将被四舍五入。来自微软

回答by meskobalazs

If you need it as a string, this should work:

如果你需要它作为一个字符串,这应该工作:

select format(round(255.87908765444,2), 'N2');

回答by Dudi Konfino

use string function substring & char index

使用字符串函数子字符串和字符索引

select SUBSTRING(convert(varchar(20),round(255.87908765444,2)),
                 1,
                 CHARINDEX('.',convert(varchar(20),255.87908765444))+2)

回答by Rejwanul Reja

Yes We can use the above solution is..

是的我们可以使用上面的解决方案是..

ROUND(CAST(psd.Price AS DECIMAL(20,4)), 2)

回答by Hitesh

select round(convert(decimal(18,2),255.87908765444),2)