c# - How to convert a string to decimal

String to decimal
we can convert or parse a string to decimal value by using Convert.ToDecimal(String) method and Decimal.Parse(String) method.

Convert.ToDecimal(String) method allow us to convert a specified string representation of a number to its equivalent decimal number. this method require a string type parameter named 'value' which contains a number to convert. the method return a decimal data type number that is equivalent to the number in string parameter. it return zero (0), if parameter value is null.

Decimal.Parse(String) method also convert a string representation of a number to its equivalent decimal number. the required parameter name is 's' which data type is string. the method return a decimal value.

the following .net c# example code demonstrate us how can we convert or parse a string instance to its equivalent decimal value in an asp.net application.

<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>    
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
        //this section create a string variable.    
        string decimalValueString = "125.99";

        Label1.Text = "string of decimal value..................<br />";
        Label1.Text += decimalValueString;

        //this line convert/parse/transform string to decimal.  
        decimal decimalValue = Decimal.Parse(decimalValueString);

        //convert string value to decimal with culture.  
        decimal decimalValue2 = Convert.ToDecimal(decimalValueString, System.Globalization.CultureInfo.InvariantCulture);

        Label1.Text += "<br /><br />converted decimal from string.........<br />";
        Label1.Text += decimalValue;

        Label1.Text += "<br />converted decimal from string with culture.........<br />";
        Label1.Text += decimalValue2;

        Label1.Text += "<br />converted decimal and add 1 .........<br />";
        Label1.Text += decimalValue + 1;
<html xmlns="http://www.w3.org/1999/xhtml">    
<head id="Head1" runat="server">    
    <title>c# example - string to decimal</title>    
    <form id="form1" runat="server">    
        <h2 style="color:MidnightBlue; font-style:italic;">    
            c# example - string to decimal
        <hr width="550" align="left" color="Gainsboro" />    
        <br /><br />  
            Text="string to decimal"    
More c# examples