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
Convert.ToDecimal Method in C#
The Convert.ToDecimal() method in C# converts a specified value to a decimal number. This method can convert various data types including strings, integers, floating-point numbers, and other numeric types to the decimal type, which provides high precision for financial and monetary calculations.
Syntax
Following are the common syntax forms for Convert.ToDecimal() −
decimal result = Convert.ToDecimal(value); decimal result = Convert.ToDecimal(stringValue, IFormatProvider);
Parameters
-
value − The value to convert to decimal. Can be string, int, double, float, bool, or other convertible types.
-
IFormatProvider − Optional culture-specific formatting information for string conversions.
Return Value
Returns a decimal equivalent of the specified value, or 0 if the value is null.
Converting String to Decimal
The most common use case is converting string representations of numbers to decimal values −
Example
using System;
public class Demo {
public static void Main() {
decimal decimalVal;
string stringVal = "2,345.26";
decimalVal = Convert.ToDecimal(stringVal);
Console.WriteLine("String converted to decimal = {0}", decimalVal);
string negativeVal = "-1234.567";
decimal negativeDecimal = Convert.ToDecimal(negativeVal);
Console.WriteLine("Negative string to decimal = {0}", negativeDecimal);
}
}
The output of the above code is −
String converted to decimal = 2345.26 Negative string to decimal = -1234.567
Converting Different Data Types
Example
using System;
public class ConvertDemo {
public static void Main() {
// Convert int to decimal
int intValue = 42;
decimal fromInt = Convert.ToDecimal(intValue);
Console.WriteLine("Int {0} to decimal: {1}", intValue, fromInt);
// Convert double to decimal
double doubleValue = 123.45;
decimal fromDouble = Convert.ToDecimal(doubleValue);
Console.WriteLine("Double {0} to decimal: {1}", doubleValue, fromDouble);
// Convert boolean to decimal
bool boolValue = true;
decimal fromBool = Convert.ToDecimal(boolValue);
Console.WriteLine("Bool {0} to decimal: {1}", boolValue, fromBool);
// Convert null returns 0
string nullString = null;
decimal fromNull = Convert.ToDecimal(nullString);
Console.WriteLine("Null to decimal: {0}", fromNull);
}
}
The output of the above code is −
Int 42 to decimal: 42 Double 123.45 to decimal: 123.45 Bool True to decimal: 1 Null to decimal: 0
Handling Format Exceptions
When converting invalid strings, Convert.ToDecimal() throws a FormatException. Use try-catch blocks for error handling −
Example
using System;
public class ExceptionDemo {
public static void Main() {
string[] testValues = {"123.45", "invalid", "999.999", "abc123"};
foreach (string value in testValues) {
try {
decimal result = Convert.ToDecimal(value);
Console.WriteLine("'{0}' converted to: {1}", value, result);
}
catch (FormatException) {
Console.WriteLine("'{0}' is not a valid decimal format", value);
}
}
}
}
The output of the above code is −
'123.45' converted to: 123.45 'invalid' is not a valid decimal format '999.999' converted to: 999.999 'abc123' is not a valid decimal format
Comparison with Other Conversion Methods
| Method | Null Handling | Exception on Invalid |
|---|---|---|
| Convert.ToDecimal() | Returns 0 | FormatException |
| decimal.Parse() | ArgumentNullException | FormatException |
| decimal.TryParse() | Returns false | Returns false |
Conclusion
The Convert.ToDecimal() method provides a versatile way to convert various data types to decimal values. It handles null values gracefully by returning 0, but throws exceptions for invalid formats, making it suitable for scenarios where you expect valid input or want explicit error handling.
