Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - NUMBERS


As we have seen in the Data Types topic, there are two types of Numbers supported by C#. Integers and floating point numbers.


Integers are the whole numbers (i.e. 7 or 5 or 2.14).


Any whole number(Be it -4 or 45) can be represented by the Int datatype


Integers


Integers are the whole numbers (i.e. 7 or 5 or 2.14).


Below are the data types used for Integers.

  1. int Data Type



    The Data Type for a whole number is int.

    The Int data type is used to store whole numbers from -2147483648 to 2147483647. And takes 4 bytes of memory.

    Example :



    public class MyApplication
    {
    	public static void Main(string[] args)
    	{
    		int x = -34535;
    		System.Console.WriteLine(x);
    	}
    }
    


    Output :



      -34535

  2. uint Data Type



    The Data Type for a whole number is uint.

    The Int data type is used to store whole numbers from 0 to 4,294,967,295. And takes 4 bytes of memory.

    Example :



    public class MyApplication
    {
    	public static void Main(string[] args)
    	{
    		uint x = 34535;
    		System.Console.WriteLine(x);
    	}
    }
    


    Output :



      34535

  3. long Data Type



    Say, we have a large value 3372036854775808.

    Well! In this case int data type will not be able to hold that value.

    And in this scenario, we need a long data type.

    A long data type is used to store whole numbers from -9223372036854775808 to 9223372036854775808.

    And takes 8 bytes of memory.

    For example 3372036854775808.

    Example :



    public class MyApplication
    {
    	public static void Main(string[] args)
    	{
    		long x = 3372036854775808;
    		System.Console.WriteLine(x);
    	}
    }
    


    Output :



      3372036854775808


    So, in the above example, we have created a variable x of type long. And assigned a large value 3372036854775808 to it.
    Note : You can also use suffix L with the value 3372036854775808, i.e. 3372036854775808L to denote the value is of long data type. However, it is completely optional.

  4. ulong Data Type



    A ulong data type is used to store whole numbers from 0 to 18,446,744,073,709,551,615. And takes 8 bytes of memory.

    For example 3372036854775808.

    Example :



    public class MyApplication
    {
    	public static void Main(string[] args)
    	{
    		long x = 3372036854775808;
    		System.Console.WriteLine(x);
    	}
    }
    


    Output :



      3372036854775808

  5. byte Data Type



    The Byte Data Type is used to hold whole numbers between 0 to 255.

    Say, there is a small number i.e. 5. So, to save space we can use Byte Data Type.

    Example :



    public class MyApplication
    {
    	public static void Main(string[] args)
    	{
    		byte x = 5;
    		System.Console.WriteLine(x);
    	}
    }
    


    Output :



      5

  6. sbyte Data Type



    The sbyte Data Type is used to hold numbers between -128 to 127.

    Say, there is a small number i.e. -5. So, to save space we can use Byte Data Type.

    Example :



    public class MyApplication
    {
    	public static void Main(string[] args)
    	{
    		sbyte x = -5;
    		System.Console.WriteLine(x);
    	}
    }
    


    Output :



      -5

  7. short Data Type



    The short Data Type is used to hold whole numbers between -32768 to 32767.

    Say, there is a number i.e. 2768. So, to save space we can use Short Data Type.

    Example :



    public class MyApplication
    {
    	public static void Main(string[] args)
    	{
    		short x = -2768;
    		System.Console.WriteLine(x);
    	}
    }
    


    Output :



      -2768

    Note : We can still use int with numbers 5 and 2768.

  8. ushort Data Type



    The ushort Data Type is used to hold whole numbers between 0 to 65,535.

    Say, there is a number i.e. 2768. So, to save space we can use ushort Data Type.

    Example :



    public class MyApplication
    {
    	public static void Main(string[] args)
    	{
    		ushort x = 2768;
    		System.Console.WriteLine(x);
    	}
    }
    


    Output :



      2768

  9. Float Data Type



    A float data type is used to hold floating point numbers. And is used to hold the floating point numbers of 32 bit.

    For example 5.987.

    Example :



    public class MyApplication
    {
    	public static void Main(string[] args)
    	{
    		float x = 5.987F;
    		System.Console.WriteLine(x);
    	}
    }
    


    Output :



      5.987


    So, in the above example, we have created a variable x of type float. And assigned the value 5.987 to it.

    float x = 5.987F;


    Just note, we have mentioned 5.987F instead of the number 5.987. This is an additional step you need to write to tell C# that 5.987 is a Floating point number.

  10. 0. double Data Type



    A double data type is used to hold floating point numbers. And is used to hold the floating point numbers of 64 bit.

    It is almost same as Float data type. Just that it can hold a floating point number of a larger size.

    For example 5.987434343434343434343.

    Example :



    public class MyApplication
    {
    	public static void Main(string[] args)
    	{
    		double x = 5.98743486215732D;
    		System.Console.WriteLine(x);
    	}
    }
    


    Output :



      5.98743486215732


    So, in the above example, we have created a variable x of type Double. And assigned the value 5.98743486215732 to it.

    double x = 5.98743486215732D;