Decimal - ToMoney
A Decimal extension method that converts the decimal to a money.
public static void Main() { decimal[] values = { 32.7865m, 7.03m, 7.646m, 0.999m, -0.12m, -7.1m, -7.6m, -32.9012m }; Console.WriteLine(" Value Money\n"); foreach (decimal value in values) { // C# Extension Method: Decimal - ToMoney Console.WriteLine("{0,7} {1,16}", value, value.ToMoney()); } }
View Source
using System; public static partial class Extensions { /// <summary> /// A Decimal extension method that converts the @this to a money. /// </summary> /// <param name="this">The @this to act on.</param> /// <returns>@this as a Decimal.</returns> public static Decimal ToMoney(this Decimal @this) { return Math.Round(@this, 2); } }