Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Decimal.ToOACurrency() Method in C#
The Decimal.ToOACurrency() method in C# converts a decimal value to an OLE Automation Currency value, which is represented as a 64-bit signed integer. OLE Automation Currency is a fixed-point data type that stores currency values with 4 decimal places of precision, making it suitable for financial calculations where precision is critical.
The method multiplies the decimal value by 10,000 to preserve 4 decimal places in the integer representation. This format is commonly used in COM interop scenarios and legacy systems that require OLE Automation compatibility.
Syntax
Following is the syntax for the Decimal.ToOACurrency() method −
public static long ToOACurrency(decimal val);
Parameters
-
val − The decimal number to convert to OLE Automation Currency format.
Return Value
Returns a 64-bit signed integer (long) representing the OLE Automation Currency value.
How It Works
The conversion process multiplies the decimal value by 10,000 to maintain 4 decimal places of precision in the integer format. For example, a decimal value of 95.0 becomes 950000, and 9587.567 becomes 95875670.
Using ToOACurrency() with Whole Numbers
When converting whole decimal numbers, the method appends four zeros to represent the preserved decimal places −
using System;
public class Demo {
public static void Main() {
decimal val = 95;
Console.WriteLine("Decimal value = " + val);
long res = Decimal.ToOACurrency(val);
Console.WriteLine("OLE Currency value = " + res);
// Converting back to verify
decimal back = Decimal.FromOACurrency(res);
Console.WriteLine("Converted back = " + back);
}
}
The output of the above code is −
Decimal value = 95 OLE Currency value = 950000 Converted back = 95
Using ToOACurrency() with Decimal Values
When converting decimal numbers with fractional parts, the method preserves up to 4 decimal places −
using System;
public class Demo {
public static void Main() {
decimal val1 = 9587.567m;
decimal val2 = 123.456789m;
Console.WriteLine("Decimal value 1 = " + val1);
long res1 = Decimal.ToOACurrency(val1);
Console.WriteLine("OLE Currency value 1 = " + res1);
Console.WriteLine("Decimal value 2 = " + val2);
long res2 = Decimal.ToOACurrency(val2);
Console.WriteLine("OLE Currency value 2 = " + res2);
// Note: Only 4 decimal places are preserved
Console.WriteLine("Back from OLE: " + Decimal.FromOACurrency(res2));
}
}
The output of the above code is −
Decimal value 1 = 9587.567 OLE Currency value 1 = 95875670 Decimal value 2 = 123.456789 OLE Currency value 2 = 1234568 Back from OLE: 123.4568
Common Use Cases
-
COM Interop − Converting C# decimal values for use with COM components that expect OLE Currency format.
-
Legacy System Integration − Working with older systems that use OLE Automation Currency for financial data.
-
Database Storage − Storing currency values in systems that use the OLE Currency format internally.
Conclusion
The Decimal.ToOACurrency() method converts decimal values to OLE Automation Currency format by multiplying by 10,000 and returning a 64-bit integer. This method is primarily used for COM interop and legacy system integration where OLE Currency format is required.
