C Sharp ( C# ) Decimal Data Type


The Decimal datatype is used in C# to accept number (both as plain integers and fractional values) but is mostly used for monetary calculations. The decimal type utilizes 128 bits to represent values within the range 1E–28 to 7.9E+28. The Decimal datatype eliminates most of the rounding errors that could happen in other datatypes. Thats the reason its mostly used for money calculations.

While assigning a value to the decimal datatype, suffix the value by "M" or "m".

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            decimal price = 25.8M;
            decimal discountpercent = 10;
            decimal actualprice;

            actualprice = price - (price * discountpercent / 100);
           
            Console.WriteLine("The final price of ${0} price product after {1} percent discount is : ${2}",price,discountpercent,actualprice);
            Console.ReadLine();
        }
    }
}

Output:

C Sharp Datatypes Home Page

No comments: