开发者

C# IFormatable ToString("0.0000")

开发者 https://www.devze.com 2022-12-17 13:39 出处:网络
I have class and I want to 开发者_如何学运维reproduce the functionality associated with ToString(\"0.0000\") as well as some other numerical formatting stuff.How can this be done? class MyNumber : IFo

I have class and I want to 开发者_如何学运维reproduce the functionality associated with ToString("0.0000") as well as some other numerical formatting stuff. How can this be done?


class MyNumber : IFormattable
{
   decimal value;
   public MyNumber(decimal value)
   { this.value = value; }

   string IFormattable.ToString(string format, IFormatProvider formatProvider)
   { return value.ToString(format, formatProvider); }

   public string ToString(string format)
   { return ((IFormattable)this).ToString(format, System.Globalization.CultureInfo.CurrentCulture); }
}

class Program
{
   static void Main(string[] args)
   {
      MyNumber num = new MyNumber(3.1415926m);
      Console.WriteLine(num.ToString("0.0000"));
      Console.WriteLine("{0:0.0000}", num);
   }
}


Regular expressions are probably your best bet.

0

精彩评论

暂无评论...
验证码 换一张
取 消