在查询中格式化SQL monny字段 [英] Formatting SQL monny field in query
问题描述
SQL字段;
ID Int NO NULL
价格,Monny
C#
const string query =SELECT Price FROM tabel WHERE ID = @ID
TX_IDText = Convert.ToString(reader [ID]);
TX_Price.Text = Convert.ToString(reader [Price]);
示例1
TX_Price显示1.0000
但这个镜头是1.00
例2
TX_Price显示1.5489
但这个镜头是1.55
如何在查询中更改此内容?
我尝试了什么:
我找到了这个页面,但我不知道如何申请我;
http://sqlusa.com/bestpractices2005/moneyformat/
SQL fields;
ID Int NO NULL
Price, Monny
C#
const string query = "SELECT Price FROM tabel WHERE ID = @ID
TX_IDText = Convert.ToString(reader["ID"]);
TX_Price.Text = Convert.ToString(reader["Price"]);
Example 1
TX_Price shows 1.0000
But this shot be 1.00
Example 2
TX_Price shows 1.5489
But this shot be 1.55
How can I change this in the query?
What I have tried:
I found this page, but I do not know how to apply i;
http://sqlusa.com/bestpractices2005/moneyformat/
推荐答案
基本上,不要 - money字段(和任何其他数字字段,包括整数,日期,浮点数......)没有格式 - 当你将它们转换为字符串时它们只获取一个,基本上 - 然后它们不能用于任何其他事情。
我强烈建议您在演示软件中对数字字段进行格式化,而不是在DB级别:它更灵活,并且能够更好地响应用户的需求。
但是......你可以通过将其转换为DECIMAL字段来实现:
Basically, don't - money fields (and any other numeric field, including integers, dates, floats...) don't have "formats" - they only acquire one when you convert them to a string, basically - and then they aren't usable for anything else.
I'd strongly suggest that you do formatting of numeric fields in your presentation software, not at DB level: it's a lot more flexible, and much better able to respond to what the user wants.
But...you can do it by converting it to a DECIMAL field:
SELECT CONVERT(DECIMAL(7,2),MyMoneyColumn) FROM MyTable
double DD;
string SS;
SS = (Double.TryParse(dr["FIELD"].ToString(), out DD) ? DD.ToString("0.00") : "");
listitem.SubItems.Add(SS);
这篇关于在查询中格式化SQL monny字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!