小数点/货币字段保留两位小数
我的数据库中有一个包含货币字段的表。我已经创建了实体并为该货币字段创建了小数属性。当该字段的值显示在 My MVC3 视图上时,它在小数点后有四个零 0000,如下所示:5489.0000。我只想显示两个 00 或小数位。我该如何解决它。为什么即使我将属性声明为小数,它仍显示四位小数。
请建议。
I have a table with money field in my database. I have created entity and created decimal property for that money field. When the value of that field is displayed on My MVC3 view, It has four zeros 0000 after decimal like this : 5489.0000. I want to show only two 00 or decimal places. How can I fix it. Why it is showing four decimal places even I declared property as decimal.
Please suggest.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SQL Server
money
数据类型内部是一个 64 位整数,隐含小数位数为 4 位。用《在线图书》的话说,它的精确度是“精确到货币单位的万分之一”。它大致相当于decimal(19,4)
。使用 4 而不是 2 的原因是为了保持算术结果的精度。您的普通货币价值的小数位数为 2(例如 $3.27)。两个小数点后两位数字的乘法或除法可得到精确到小数点后 4 位的结果:9.23 除以 3.27 得到的结果为 2.82262996941896(大约)。您可以将结果精确到您想要的任何精度(小数位数)。然而,结果仅精确到小数点后 4 位 (2.8226),因为原始值仅精确到小数点后 2 位。该测量精确到指定最小单位的 1/2 以内 (+/- 0.005)。
但我离题了。
由于 SQL Server
money
值的隐含小数位数为 4,ADO.Net 会将该值转换为小数位数为 4 的 System.Decimal。并且由于 System.Decimal 跟踪小数位数,因此当您将其转换为字符串,您将得到 4 个小数位。要减少数量,您可以
Decimal.Round()
重载对其进行舍入,或(3.27M).ToString("0.00") ;
。希望
这个程序能够
产生您所期望的结果:
干杯,
N。
The SQL Server
money
datatype internally is a 64-bit integer with an implied scale of 4 decimal places. To quote Books Online, it is accurate "to ten-thousandsth of a currency unit." It is, the rough equivalent of adecimal(19,4)
.The reason for the scale of 4 rather than 2 is to maintain precision in the results of arithmetic. Your ordinary currency value has a scale of 2 (e.g. $3.27) Multiplication or division of two numbers scaled to 2 decimal places gives a results that is precise to 4 decimal places: 9.23 divided by 3.27 yields a result of 2.82262996941896 (approximately). You can carry the result to whatever accuracy (number of decimal places) you desire. However, the result is only precise to 4 decimal places (2.8226) as the original values were only precise to 2 decimal places. That measurement is precise to within 1/2 of the smallest unit specified (+/- 0.005).
But I digress.
As a result of a SQL Server
money
value having an implied scale of 4, ADO.Net converts the value to a System.Decimal with a scale of 4. And since System.Decimal tracks scale, when you convert it to string, you get 4 decimal places.To get fewer, you can
Decimal.Round()
overload, or(3.27M).ToString("0.00") ;
.Hope this helps.
This program:
Produces what you'd expect:
Cheers,
N.
您必须格式化字符串。
如果您想显示金钱,您可以做的一件事是:
}
输出:
输出
我的金额 = $1.00
您的金额 = $9,999,999,999,999,999,999,999,999,999.00
{0:C} 是货币 格式
希望这会有所帮助!
You have to format the string.
One thing you can do if it money you want to display is:
}
OUTPUT:
Output
My amount = $1.00
Your amount = $9,999,999,999,999,999,999,999,999,999.00
the {0:C} is the currency Format
Hope this helps!
这是您需要了解的有关格式化字符串的所有信息。
http://blog.stevex.net/string-formatting-in-csharp/
Here is everything you need to know about formatting strings.
http://blog.stevex.net/string-formatting-in-csharp/
我用过这个并且它有效:
I used this and it worked: