小数点/货币字段保留两位小数

发布于 2024-11-08 21:23:22 字数 163 浏览 0 评论 0原文

我的数据库中有一个包含货币字段的表。我已经创建了实体并为该货币字段创建了小数属性。当该字段的值显示在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北城半夏 2024-11-15 21:23:22

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") ;

希望

这个程序能够

namespace Sandbox
{
  using System ;
  class Program
  {
    static void Main( string[] args )
    {
      decimal pi     = (decimal) Math.PI ;
      string  piText = pi.ToString("0.00");
      Console.WriteLine("PI to 2 decimal places is {0} one way, and {1:0.00} another" , piText , pi ) ;
      return;
    }
  }
}

产生您所期望的结果:

PI to 2 decimal places is 3.14 one way, and 3.14 another

干杯,

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 a decimal(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

  • Round it before conversion, using the appropriate Decimal.Round() overload, or
  • Format it as desired (eg. (3.27M).ToString("0.00") ;.

Hope this helps.

This program:

namespace Sandbox
{
  using System ;
  class Program
  {
    static void Main( string[] args )
    {
      decimal pi     = (decimal) Math.PI ;
      string  piText = pi.ToString("0.00");
      Console.WriteLine("PI to 2 decimal places is {0} one way, and {1:0.00} another" , piText , pi ) ;
      return;
    }
  }
}

Produces what you'd expect:

PI to 2 decimal places is 3.14 one way, and 3.14 another

Cheers,

N.

比忠 2024-11-15 21:23:22

您必须格式化字符串。

如果您想显示金钱,您可以做的一件事是:

static void Main () 
{
    decimal x = 0.999m;
    decimal y = 9999999999999999999999999999m;
    Console.WriteLine("My amount = {0:C}", x);
    Console.WriteLine("Your amount = {0:C}", y);
}

}

输出:
输出

我的金额 = $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:

static void Main () 
{
    decimal x = 0.999m;
    decimal y = 9999999999999999999999999999m;
    Console.WriteLine("My amount = {0:C}", x);
    Console.WriteLine("Your amount = {0:C}", y);
}

}

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!

江南烟雨〆相思醉 2024-11-15 21:23:22

这是您需要了解的有关格式化字符串的所有信息。

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/

執念 2024-11-15 21:23:22

我用过这个并且它有效:

YourDecField.ToString("N")

I used this and it worked:

YourDecField.ToString("N")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文