Numbers in C#

Types

There are 3 types of numbers that are being used in C#.

Integer Numbers


In C# there is nearly a complete list of integer number types that can get used, like other languages (C, Java, Objective C and so on). But this article will talk about integer numbers of C# with their type, size and respective range.



Big Integer Numbers

Now I will show the two available big integer number types in C#, whose range is really wide in real. These are long number and ulong number as in the following:



Floating Point Numbers



Comparison between Double and Decimal Number

Now for an example of how floating point number actually work and what the major difference is between them through a small piece of a code; see the following:

Double Number

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Hello_Word

{

    class Program

    {

        static void Main(string[] args)

        {

            double sub;
            double first = 0.2f;

            double second = 1.0f;

            sub = (second - first);

            Console.WriteLine("value is: " +sub);

            Console.ReadLine();

         }

     }     

}




Decimal Number

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Hello_Word

{

    class Program

    {

        static void Main(string[] args)

        {

            decimal sub;

            decimal first = 0.2m;

            decimal second = 1.0m;

            sub = (second - first);

            Console.WriteLine("value is: " +sub);

            Console.ReadLine();

         }   
     }     

}



Note that in both cases we are taking the same value but the output is quite different from each other. It is because for decimal numbers the calculation is by direct calculation since it deals with base 10, so the output is 0.8.

But in the case of the double number our value is 0.79999 something because it deals with base 2 numbers.

So now the question arises, what is the major difference between these two floating point numbers and where can we use them depending on their respective range and their methods?

Explanation

I am explaining all these questions with this simple table, through which you can easily understand what to use and where to use of them.


Up Next
    Ebook Download
    View all
    Learn
    View all