Decimal - ToOACurrency

Converts the specified value to the equivalent OLE Automation Currency value, which is contained in a 64-bit signed integer.

Try it

const string dataFmt = "{0,31}{1,27}";

public static void Main()
{
    decimal[] values = { 0M, 1M, 1.0000000000000000000000000000M,
                        100000000000000M, 100000000000000.00000000000000M,
                        10000000000000000000000000000M, 0.000000000123456789M,
                        0.123456789M, 123456789M, 123456789000000000M, 4294967295M,
                        18446744073709551615M, -79.228162514264337593543950335M,
                        -79228162514264.337593543950335M };

    Console.WriteLine(dataFmt, "Decimal value", "OA Currency or Exception");
    Console.WriteLine(dataFmt, "-------------", "------------------------");

    foreach (var value in values)
    {
        // Catch the exception if ToOACurrency( ) throws one.
        try
        {
			// C# Extension Method: Decimal - ToOACurrency
            long oaCurrency = value.ToOACurrency();
            Console.WriteLine(dataFmt, value, oaCurrency);
        }
        catch (Exception ex)
        {
            Console.WriteLine(dataFmt, value,
                GetExceptionType(ex));
        }
    }
}

// Get the exception type name; remove the namespace prefix.
public static string GetExceptionType(Exception ex)
{
    string exceptionType = ex.GetType().ToString();
    return exceptionType.Substring(
        exceptionType.LastIndexOf('.') + 1);
}

View Source
using System;

public static partial class Extensions
{
    /// <summary>
    ///     Converts the specified  value to the equivalent OLE Automation Currency value, which is contained in a 64-bit
    ///     signed integer.
    /// </summary>
    /// <param name="value">The decimal number to convert.</param>
    /// <returns>A 64-bit signed integer that contains the OLE Automation equivalent of .</returns>
    public static Int64 ToOACurrency(this Decimal value)
    {
        return Decimal.ToOACurrency(value);
    }
}