Frage

Ich habe Unterricht und ich möchte die Funktionalität reproduzieren ToString("0.0000") sowie einige andere numerische Formatierende. Wie kann das getan werden?

War es hilfreich?

Lösung

Regelmäßige Ausdrücke sind wahrscheinlich die beste Wahl.

Andere Tipps

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);
   }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top