Numeric Data Type In C Sharp Day 19

Lecture 19 : Numeric Data Type In C Sharp


Numeric Data Type :
  • The numeric types represent integer, decimal and double numbers.

  • In this article with a quick reference to the numeric data types. 

  • For integer types, the declaration keyword, a description of the type, the range of possible values and the number of bits used to represent the value are given. 

  • For non-integers, the scale and the number of digits of accuracy are given, rather than minimum and maximum

  • This allows you to determine which data type should be used for any numeric variable.

  • The Boolean type is included, which although not technically numeric, fits well in this table. 

  • The Boolean type can hold either true or false.

Integer :
  • The int data type is 32-bit signed integer.

  • It can store numbers from -2,147,483,648 to 2,147,483,647

  • The int keyword is an alias of Int32 struct in .NET.

  • The uint is 32-bit unsigned integer. 

  • The uint keyword is an alias of UInt32 struct in .NET. 

  • It can store positive numbers from 0 to 4,294,967,295

  • Optionally use U or u suffix after a number to assign it to uint variable.

Decimal :
  • The decimal data type can store fractional numbers from ±1.0 x 10-28 to ±7.9228 x 1028.

  • It occupies 16 bytes in the memory. 

  • The decimal is a keyword alias of the Decimal struct in .NET.

  • The decimal type has more precision and a smaller range than both float and double, and so it is appropriate for financial and monetary calculations.

  • Use m or M suffix with literal to make it decimal type.

Double :
  • The double data type can store fractional numbers from 1.7e−308 to 1.7e+308.

  • It occupies 8 bytes in the memory.

  • The double keyword is an alias of the Double struct in .NET.

  • Use d or D suffix with literal to make it double type.

Type Casting :
  • Type conversion is converting one type of data to another type, It is also known as Type Casting.

  • In C#, type casting has two forms −

1) Implicit :
  • The values of certain data types are automatically converted to different data types in C#.

  • Example :

           int i = 345; 
           float f = i; 
           Console.WriteLine(f);
  • In the above example, the value of an integer variable i is assigned to the variable of float type f because this conversion operation is predefined in C#.

  • However, not all data types are implicitly converted to other data types. 

  • For example, int type cannot be converted to uint implicitly.

2) Explicit :
  • It must be specified explicitly, as shown below :

  • Example :

           int i = 100; 
           uint u = (uint) i;
           Console.Write(i);
  • In the above example, integer i is converted to uint explicitly by specifying uint in the brackets (uint). 

  • This will convert an integer to uint.

Convert Function :
  • ToInt16()Converts a specified value to a 16-bit signed integer.

  • ToInt32()Converts a specified value to a 32-bit signed integer.

  • ToInt64()Converts a specified value to a 64-bit signed integer.

Program-

Output-

Comments

Popular posts from this blog

Basic Concept In C Sharp Day 16

Understanding C Sharp Day 18

Concept In Angular Day 14