我有课,我想复制与 ToString("0.0000") 以及其他一些数值格式的内容。如何才能做到这一点?

有帮助吗?

解决方案

正则表达可能是您最好的选择。

其他提示

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);
   }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top