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?
views:
131answers:
2Damnit. That was my fear. Thanks for confirming it I guess.
Ames
2010-01-22 07:35:01
Any particular reason you're trying to avoid RegEx?
Rory
2010-01-22 07:43:45
This question is about formatting, not parsing. Regex is inappropriate.
Hans Passant
2010-01-22 08:50:44
@nobugz: Ever heard of the `RegEx.Replace` method?
Rory
2010-01-22 14:56:25
A:
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);
}
}
BlueMonkMN
2010-01-22 14:57:11