Convert.ToDecimal Method in C#

ProgrammingServer Side ProgrammingCsharp

Convert a specified value to a decimal number using the Convert.ToDecimal() method.

We have a string here.

string stringVal = "2,345.26";

Now, let us use the Convert.ToDecimal() method to convert it to a decimal number.

decimal decimalVal;
decimalVal = System.Convert.ToDecimal(stringVal);

Let us now see the complete example −

Example

 Live Demo

using System;
public class Demo {
   public static void Main() {
      decimal decimalVal;
      string stringVal = "2,345.26";
      decimalVal = System.Convert.ToDecimal(stringVal);
      System.Console.WriteLine("String converted to decimal = {0} ", decimalVal);
   }
}

Output

String converted to decimal = 2345.26
raja
Published on 11-Sep-2018 12:07:36
Advertisements