[英]C# IFormatable ToString(“0.0000”)
I have class and I want to reproduce the functionality associated with ToString("0.0000")
as well as some other numerical formatting stuff. 我有课,我想重现与
ToString("0.0000")
以及其他一些数字格式设置相关的功能。 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);
}
}
正则表达式可能是最好的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.