Decimal.Parse()
is a C# method that is used to convert the string representation of a number to its decimal equivalent.
Decimal.Parse(str)
str
: The string representation of a number.This method returns a decimal value that is the decimal equivalent of str
.
// use Systemusing System;// create classclass StringToDecimal{// main methodstatic void Main(){// create stringsstring s1 = "3.333";string s2 = "4";string s3 = "-0.455";// convert to decimalConsole.WriteLine(Decimal.Parse(s1) + 1);Console.WriteLine(Decimal.Parse(s2) + 1);Console.WriteLine(Decimal.Parse(s3) + 1);}}
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.