简体   繁体   English

覆盖十进制ToString()方法

[英]Override decimal ToString() method

I have a decimal datatype with a precision of (18, 8) in my database and even if its value is simply 14.765 it will still get displayed as 14.76500000 when I use Response.Write to return its value into a webpage. 我的数据库中有一个十进制数据类型,精度为(18,8),即使它的值仅为14.765,当我使用Response.Write将其值返回到网页时,它仍将显示为14.76500000。

Is it possible to override its default ToString method to return the number in the format #,###,##0.######## so that it only displays relevant decimal places? 是否可以覆盖其默认的ToString方法,以#,###,##0.########格式返回数字,以便它只显示相关的小数位?

UPDATE UPDATE

I'm assuming that when one outputs number on a page like <%= item.price %> (where item.price is a number) that the number's ToString method is being called? 我假设当一个页面输出数字如<%= item.price %> (其中item.price是一个数字)时,正在调用该数字的ToString方法?

I'm trying to avoid having to change every instance where the value is displayed by defaulting the ToString() format somehow. 我试图通过以某种方式默认ToString()格式来避免必须更改显示值的每个实例。

您不想重写ToString() ,您想调用ToString()并指定IFormatProvider格式化String

You can't override ToString for decimal type since it's a struct . 您不能为小数类型覆盖ToString,因为它是一个结构

But you can use extension methods to do so: 但您可以使用扩展方法来执行此操作:

public static class DecimalExtensions
{
      public static string ToString(this decimal some, bool compactFormat)
      {
            if(compactFormat) 
            {
                return some.ToString("#,###,##0.########");
            }
            else
            {
                return some.ToString();
            }
      }
}

So now you can do this: 所以现在你可以这样做:

string compactedDecimalText = 16.38393m.ToString(true);

I ran into the same problem as you. 我遇到了和你一样的问题。 People wanted decimals to be formatted in crazy custom ways throughout the application. 人们希望在整个应用程序中以疯狂的自定义方式格式化小数。

My first idea was to create a wrapper around Decimal and override the ToString() method. 我的第一个想法是在Decimal周围创建一个包装器并覆盖ToString()方法。

Although you cannot inherit from Decimal, you can create a type that implicitly casts to and from a Decimal. 虽然您不能从Decimal继承,但您可以创建一个隐式转换为Decimal的类型。

This creates a facade in front of Decimal, and gives you a place to write custom formatting logic. 这会在Decimal前面创建一个外观,并为您提供编写自定义格式逻辑的位置。

Below is my possible solution, let me know if this works or if this is a horrible idea. 以下是我可能的解决方案,让我知道这是否有效或者这是一个可怕的想法。

public struct Recimal
{

    private decimal _val;
    public Recimal(decimal d) { _val = d; }

    public static implicit operator Recimal(decimal d)
    {
        return new Recimal(d);
    }

    public static implicit operator Decimal(Recimal d)
    {
        return d._val;
    }

    public override string ToString()
    {
        //if negative, show the minus sign to the right of the number
        if (_val < 0)
            return String.Format("{0:0.00;0.00-;}", _val);

        return _val.ToString();
    }

}

It works just like a Decimal: 它就像一个十进制:

        Decimal d = 5;
        Recimal r = -10M;
        Recimal result = r + d;

        //result: 5.00-
        textBox1.Text = result.ToString();

Why not use string.Format() directly on double ? 为什么不直接在double上使用string.Format()

Here are some references for the formatting options: 以下是格式化选项的一些参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM