Convert.ToDecimal(String, IFormatProvider) Method in C#

The Convert.ToDecimal(String, IFormatProvider) method in C# converts the specified string representation of a number to an equivalent decimal number, using the specified culture-specific formatting information. This overload is particularly useful when dealing with different number formats across various cultures and locales.

Syntax

Following is the syntax −

public static decimal ToDecimal(string value, IFormatProvider provider);

Parameters

  • value − A string that contains a number to convert.

  • provider − An object that supplies culture-specific formatting information. If null, the current thread culture is used.

Return Value

Returns a decimal number that is equivalent to the number in value, or 0 (zero) if value is null.

Culture-Specific Number Conversion String Input "123.45" Culture Provider IFormatProvider (en-US, fr-FR, etc.) Decimal Output 123.45m Respects culture-specific decimal separators and formatting

Using Convert.ToDecimal with US Culture

Example

using System;
using System.Globalization;

public class Demo {
   public static void Main() {
      CultureInfo cultures = new CultureInfo("en-US");
      String val = "8787.50";
      Console.WriteLine("Converted Decimal value...");
      decimal res = Convert.ToDecimal(val, cultures);
      Console.WriteLine("{0}", res);
      Console.WriteLine("Type: {0}", res.GetType());
   }
}

The output of the above code is −

Converted Decimal value...
8787.50
Type: System.Decimal

Using Convert.ToDecimal with Different Cultures

Example

using System;
using System.Globalization;

public class CultureDemo {
   public static void Main() {
      string value = "1,234.56";
      
      // US culture (comma as thousands separator, dot as decimal)
      CultureInfo usCulture = new CultureInfo("en-US");
      decimal usDecimal = Convert.ToDecimal(value, usCulture);
      Console.WriteLine("US Culture: {0}", usDecimal);
      
      // French culture example with appropriate format
      string frenchValue = "1234,56";
      CultureInfo frCulture = new CultureInfo("fr-FR");
      decimal frDecimal = Convert.ToDecimal(frenchValue, frCulture);
      Console.WriteLine("French Culture: {0}", frDecimal);
      
      // Using invariant culture
      string invariantValue = "9999.99";
      decimal invDecimal = Convert.ToDecimal(invariantValue, CultureInfo.InvariantCulture);
      Console.WriteLine("Invariant Culture: {0}", invDecimal);
   }
}

The output of the above code is −

US Culture: 1234.56
French Culture: 1234.56
Invariant Culture: 9999.99

Error Handling with Invalid Input

Example

using System;
using System.Globalization;

public class ErrorHandlingDemo {
   public static void Main() {
      CultureInfo culture = new CultureInfo("en-US");
      
      // Valid conversion
      try {
         decimal result1 = Convert.ToDecimal("123.45", culture);
         Console.WriteLine("Valid: {0}", result1);
      } catch (Exception ex) {
         Console.WriteLine("Error: {0}", ex.Message);
      }
      
      // Invalid conversion
      try {
         decimal result2 = Convert.ToDecimal("invalid", culture);
         Console.WriteLine("Invalid: {0}", result2);
      } catch (FormatException ex) {
         Console.WriteLine("Format Error: {0}", ex.Message);
      }
      
      // Null value (returns 0)
      decimal result3 = Convert.ToDecimal(null, culture);
      Console.WriteLine("Null input: {0}", result3);
   }
}

The output of the above code is −

Valid: 123.45
Format Error: Input string was not in a correct format.
Null input: 0

Conclusion

The Convert.ToDecimal(String, IFormatProvider) method provides culture-aware conversion of string values to decimal numbers. It respects different number formatting conventions across cultures and returns 0 for null input, making it essential for internationalized applications that handle numeric data from various locales.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements