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


The Convert.ToDecimal() method in C# converts the specified string representation of a number to an equivalent decimal number, using the specified culture-specific formatting information.

Syntax

Following is the syntax −

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

Above, Val is a string that contains a number to convert, whereas the provider is an object that supplies culture-specific formatting information.

Example

Let us now see an example to implement the Convert.ToDecimal() method −

using System;
using System.Globalization;
public class Demo {
   public static void Main() {
      CultureInfo cultures = new CultureInfo("en-US");
      String val = "8787";
      Console.WriteLine("Converted Decimal value...");
      decimal res = Convert.ToDecimal(val, cultures);
      Console.Write("{0}", res);
   }
}

Output

This will produce the following output −

Converted Decimal value...
8787
raja
Published on 05-Nov-2019 09:35:06
Advertisements