Convert.ToDecimal Method in C#


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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 23-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements