Parse string to decimal in CSharp

Description

The following code shows how to parse string to decimal.

Example


   /*w w  w. ja v a  2 s  .  c  o  m*/
using System;

public class Class1 {
    public static void Main(string[] args) {

        string sPrincipal = "123.123";
        decimal mPrincipal = decimal.Parse(sPrincipal);

        Console.Write("Enter interest:");
        string sInterest = "0.123";
        decimal mInterest = decimal.Parse(sInterest);

        decimal mInterestPaid;
        mInterestPaid = mPrincipal * (mInterest / 100);

        decimal mTotal = mPrincipal + mInterestPaid;

        Console.WriteLine("Principal     = " + mPrincipal);
        Console.WriteLine("Interest      = " + mInterest + "%");
        Console.WriteLine("Interest paid = " + mInterestPaid);
        Console.WriteLine("Total         = " + mTotal);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var