Numbers in C# with Examples

In C# there are two groups of numbers

  • Integer Types
  • Float Types

Integer Types

Integers are the simplest type of value for the pc to store. Each value will map onto a specific pattern of bits. the only issue is one of range. the larger the value the larger the number of bits that you simply got to represent it. C# provides a spread of integer types, counting on the range of values you'd like to store.

  • Byte
  • Short
  • Int
  • long

Examples:

int integer_number = 100000;
byte byte_variable = 200;
short short_variable = 32767;

Byte

In C#, Byte Struct is used to represent 8-bit unsigned integers. The Byte is an immutable value type and the range of Byte is from 0 to 255. This class allows you to create Byte data types and you'll perform mathematical and bitwise operations on them like addition, subtraction, multiplication, division, XOR, AND etc.

Example 1: 

using System;   
public class EntryPoint {   
    // Main method 
    static public void Main() 
    { 
        // minimum and maximum value of Byte 
        Console.WriteLine("The minimum value "+ 
                 "of Byte: {0}", Byte.MinValue); 
  
        Console.WriteLine("The maximum value "+ 
                 "of Byte: {0}", Byte.MaxValue); 
    } 
} 

Example 2: 

using System;

public class EntryPoint
{     
    static public void Main()
    {       
        byte value1 = 32;
        byte value2 = 40;
        string value3 = "80";        
        Console.WriteLine("Comparison - ",
                         value1.CompareTo(value2));
        Console.WriteLine(value1.ToString());
        Console.WriteLine(Byte.Parse(value3));       
    }
}

Short

short is a keyword that is used to declare a variable that can store a signed integer value from the range -32, 768 to 32, 767. It is an alias of the System.Int16.

using System;
public class EntryPoint
{     
    static public void Main()
    {
        // variable declaration 
        short number = 20;
        //value 
        Console.WriteLine("number: " + number);
        // size 
        Console.WriteLine("Size of a short variable: " + sizeof(short));
    }
}

Output:

number: 20
Size of a short variable: 2

Int

int is a keyword that is used to declare a variable that can store an integral type of value (signed integer) the range from -2,147,483,648 to 2,147,483,647. It is an alias of the System.Int32.

using System;

public class EntryPoint
{     
    static public void Main()
    {
        // variable declaration 
        int number = -245;
        //value 
        Console.WriteLine("number: " + number);
        //size 
        Console.WriteLine("Size of an int variable: " + sizeof(int));
    }
}

Output:

number: -245
Size of an int variable: 4

Long

long is a keyword that is used to declare a variable that can store a signed integer value from the range of  -9,223,372,036,854,775,808  to  9,223,372,036,854,775,807.  It is an alias of the System.Int64.

using System;
public class EntryPoint
{     
    static public void Main()
    {
        // variable declaration 
        long number = 1234;
        //value 
        Console.WriteLine("number: " + number);
        //size 
        Console.WriteLine("Size of a long variable: " + sizeof(long));
    }
}

Output:

number: 1234
Size of a long variable: 8

Float Types

In C#, a Float is a whole number with one or more decimal points it can be positive or negative. Let’s discuss data types of float.

Data Type Range
Float 3.4e−038 to 3.4e+038
Double 1.7e−308 to 1.7e+308
Decimal ±1.0 x 10-28 to ±7.9228 x 1028

Examples:

float Float_Variable = 123456.5F;
double double_variable = 12345678912345.5d;
decimal decimal_variable = 123456789123456789123456789.5m;

Double

Double is a 64-bit double-precision floating-point type. It has 14 – 15 digit Precision. To initialize a double variable, use the suffix d or D. But it is not mandatory to use suffix because by default floating data types are the double type.

using System;
public class EntryPoint
{     
    static public void Main()
    {
        // variable declaration 
        double number = 1234.123456d;
        //value 
        Console.WriteLine("number: " + number);
        //size 
        Console.WriteLine("Size of a double variable: " + sizeof(double));
    }
}

Output:

number: 1234.123456
Size of a double variable: 8

Decimal

The decimal type is a 128-bit data type suitable for financial and monetary calculations. It has a 28-29 digit Precision. To initialize a decimal variable, use the suffix m or M. Like as, decimal number = 300.5m; If the suffix m or M will not use then it is treated as double.

using System;
public class EntryPoint
{     
    static public void Main()
    {
        // variable declaration 
        decimal number = 1.22m;
        //value 
        Console.WriteLine("number: " + number);
        //size 
        Console.WriteLine("Size of a decimal variable: " + sizeof(decimal));
    }
}

Output:

number: 1.22
Size of a decimal variable: 16