What is the Decimal.Parse() method in C#?

Overview

Decimal.Parse() is a C# method that is used to convert the string representation of a number to its decimal equivalent.

Syntax

Decimal.Parse(str)

Parameters

  • str: The string representation of a number.

Return value

This method returns a decimal value that is the decimal equivalent of str.

Example

// use System
using System;
// create class
class StringToDecimal
{
// main method
static void Main()
{
// create strings
string s1 = "3.333";
string s2 = "4";
string s3 = "-0.455";
// convert to decimal
Console.WriteLine(Decimal.Parse(s1) + 1);
Console.WriteLine(Decimal.Parse(s2) + 1);
Console.WriteLine(Decimal.Parse(s3) + 1);
}
}

Explanation

  • Lines 10 to 12: We create string variables and initialize them with string values that represent numbers.

  • Lines 15 to 17: We call the Decimal.Parse() method on the strings we created and print the results in decimal by adding 1 to them.

Free Resources