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
C# Program to convert an Int32 value to a decimal
To convert an Int32 value to a decimal in C#, you can use the Convert.ToDecimal() method or implicit casting. An Int32 represents a 32-bit signed integer, while decimal provides higher precision for financial and monetary calculations.
Syntax
Following is the syntax for converting Int32 to decimal using Convert.ToDecimal() −
decimal result = Convert.ToDecimal(intValue);
Following is the syntax for implicit casting −
decimal result = intValue;
Using Convert.ToDecimal() Method
The Convert.ToDecimal() method explicitly converts an Int32 value to a decimal type −
using System;
public class Demo {
public static void Main() {
int val = 2923;
decimal decVal = Convert.ToDecimal(val);
Console.WriteLine("Original Int32 value: {0}", val);
Console.WriteLine("Converted decimal value: {0:N2}", decVal);
Console.WriteLine("Type of original: {0}", val.GetType());
Console.WriteLine("Type of converted: {0}", decVal.GetType());
}
}
The output of the above code is −
Original Int32 value: 2923 Converted decimal value: 2,923.00 Type of original: System.Int32 Type of converted: System.Decimal
Using Implicit Casting
Since decimal has a wider range than Int32, you can use implicit casting for conversion −
using System;
public class Demo {
public static void Main() {
int[] intValues = {100, -250, 0, 999999};
Console.WriteLine("Int32 to Decimal Conversion:");
Console.WriteLine("Int32\t\tDecimal");
Console.WriteLine("-----\t\t-------");
foreach(int value in intValues) {
decimal decimalValue = value; // Implicit casting
Console.WriteLine("{0}\t\t{1}", value, decimalValue);
}
}
}
The output of the above code is −
Int32 to Decimal Conversion: Int32 Decimal ----- ------- 100 100 -250 -250 0 0 999999 999999
Handling Large Int32 Values
Both conversion methods work seamlessly with the full range of Int32 values −
using System;
public class Demo {
public static void Main() {
int maxInt = int.MaxValue;
int minInt = int.MinValue;
decimal maxDecimal = Convert.ToDecimal(maxInt);
decimal minDecimal = minInt; // Implicit casting
Console.WriteLine("Maximum Int32: {0}", maxInt);
Console.WriteLine("As Decimal: {0:N0}", maxDecimal);
Console.WriteLine("Minimum Int32: {0}", minInt);
Console.WriteLine("As Decimal: {0:N0}", minDecimal);
}
}
The output of the above code is −
Maximum Int32: 2147483647 As Decimal: 2,147,483,647 Minimum Int32: -2147483648 As Decimal: -2,147,483,648
Comparison of Methods
| Method | Syntax | Performance | Use Case |
|---|---|---|---|
| Convert.ToDecimal() | Convert.ToDecimal(intValue) | Slightly slower | Explicit conversion, null handling |
| Implicit Casting | decimal result = intValue | Faster | Direct assignment, cleaner code |
Conclusion
Converting Int32 to decimal in C# can be done using Convert.ToDecimal() or implicit casting. Both methods preserve the integer value exactly, with implicit casting being more efficient and commonly preferred for simple conversions.
