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.FromOACurrency() Method in C#
The Decimal.FromOACurrency() method in C# converts a 64-bit signed integer containing an OLE Automation Currency value to its equivalent decimal representation. OLE Automation Currency stores currency values as scaled integers, where the value is multiplied by 10,000 to preserve four decimal places of precision.
Syntax
Following is the syntax for the Decimal.FromOACurrency() method −
public static decimal FromOACurrency(long val);
Parameters
val: A 64-bit signed integer that contains an OLE Automation Currency value to be converted.
Return Value
Returns a decimal value equivalent to the OLE Automation Currency value. The returned decimal has the currency value divided by 10,000.
How It Works
OLE Automation Currency represents monetary values as integers scaled by 10,000. This means a currency value of $123.45 is stored as the integer 1,234,500. The FromOACurrency() method reverses this process by dividing the input by 10,000.
Example 1 - Basic Usage
using System;
public class Demo {
public static void Main() {
long val = 978576L;
decimal res = Decimal.FromOACurrency(val);
Console.WriteLine("OLE Currency Value: " + val);
Console.WriteLine("Decimal Value: " + res);
Console.WriteLine("Conversion: " + val + " / 10000 = " + res);
}
}
The output of the above code is −
OLE Currency Value: 978576 Decimal Value: 97.8576 Conversion: 978576 / 10000 = 97.8576
Example 2 - Using Minimum Value
using System;
public class Demo {
public static void Main() {
long val = long.MinValue;
decimal res = Decimal.FromOACurrency(val);
Console.WriteLine("Long.MinValue: " + val);
Console.WriteLine("Decimal Value: " + res);
}
}
The output of the above code is −
Long.MinValue: -9223372036854775808 Decimal Value: -922337203685477.5808
Example 3 - Multiple Currency Values
using System;
public class Demo {
public static void Main() {
long[] currencyValues = { 12345L, 1000000L, 50000L, 0L };
Console.WriteLine("OLE Currency to Decimal Conversion:");
Console.WriteLine("===================================");
foreach (long currency in currencyValues) {
decimal result = Decimal.FromOACurrency(currency);
Console.WriteLine($"{currency,12} -> {result,10}");
}
}
}
The output of the above code is −
OLE Currency to Decimal Conversion:
===================================
12345 -> 1.2345
1000000 -> 100
50000 -> 5
0 -> 0
Common Use Cases
-
Legacy System Integration: Converting currency data from older OLE Automation systems.
-
COM Interop: Working with COM objects that use OLE Automation Currency format.
-
Financial Applications: Processing currency data stored in scaled integer format for precision.
Conclusion
The Decimal.FromOACurrency() method provides a reliable way to convert OLE Automation Currency values to decimal format by dividing the input by 10,000. This method is essential when working with legacy systems or COM objects that store currency values as scaled integers.
