.net 在 SQL Server 中的货币数据类型中只获取两个小数点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3509891/
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-03 14:37:59  来源:igfitidea点击:

Get only two decimal points in money datatype in SQL Server

.netsql-serversql-server-2005tsqlrounding

提问by IsmailS

SELECT ROUND(123.4567, 2)` gives me `123.4600`

But I need 123.46.

但我需要123.46.

Data type of field is money.

字段的数据类型是money。

Solution:

解决方案:

<%# DataBinder.Eval(Container.DataItem, "FieldName","{0:0.00}") %>

回答by Neil Knight

SELECT CAST(ROUND(123.4567, 2) AS MONEY)

Will do what you are after

会做你想做的事

回答by abatishchev

If applicable, format on the view layer, not on the data layer, i.e. read all the data and truncate it later (such as in C# client)

如果适用,在视图层格式化,而不是在数据层格式化,即读取所有数据并稍后截断(例如在C#客户端中)

回答by Christopher Huff

@IsmailS - To me, the larger picture is to let the data server format as much as possible, especially when it's something as simple as an inline cast. It yields less clutter in your other code. For example, casting Price, a money field:

@IsmailS - 对我来说,更大的图景是让数据服务器尽可能多地格式化,尤其是当它像内联转换一样简单时。它在您的其他代码中产生更少的混乱。例如,铸造Price,一个金钱字段:

select CAST(Price as numeric(17,2)) Price from PriceTable

select CAST(Price as numeric(17,2)) Price from PriceTable

YMMV - It's my personal experience.

YMMV - 这是我的个人经历。

回答by Abhishek Singh

SELECT FORMAT(123.4567,'N2')

It will Help You Out

它会帮助你