ASP.NET Basics (part 2): Not My Type - Playing The Numbers
(Page 5 of 10 )
Some programming languages (like PHP and VBScript) do not offer a range of numeric variables to play with, preferring instead to use one catch-all type; others (like C++ and Java) are very fussy and insist on offering you specialized data types for different purposes. In case you're wondering, yes, C# falls in the latter category.
There are broadly three kinds of numeric data types in C#: integer, floating-point and decimal.
1. Integers: These are plain-vanilla numbers like 23, 5748, -947 and 3; they are also the most commonly used type. Based on the range of the number being stored, the language has the following data types:
- sbtye: stores a number between -127 to 127
- short: stores a number between -32768 to 32767
- int: stores a number between -2147483648 to -2147483647
- long: stores a number between -9223372036854775808 to 9223372036854775807
- byte: stores a number between 0 to 255
- ushort: stores a number between 0 and 65535
- uint: stores a number between 0 and 4294967295
- ulong: stores a number between 0 and 18446744073709551615
The first four data types are often referred to as "signed" integers while the last four are referred to as "unsigned" integers. This has to do with the fact that first bit for the signed integer data types is used to store information about which side of the number line the integer lies on (0 for positive integers, 1 for negative integers). Unsigned data types assume that their values are always positive (and use the extra bit to offer a wider range of storage).
2. Floating-points: These are typically fractional numbers such as 12.5 or 3.149391239129. There are two floating-point data types.
- float: stores a number between +/- 1.5 X 10-45 to +/- 3.4 X 1038
- double: stores a number between +/- 5.0 X 10-324 to +/- 1.7 X 10308
The main difference between the two types is in the "precision", or the number of significant figures after the decimal. If you want 7 or fewer significant digits after the decimal point (32-bit precision), use a "float"; if you want between 8 and 16 significant digits after the decimal point (64-bit precision), use a "double".
3. Decimals: The "decimal" data type should be used when you require a high level of precision, since it can accurately store data up to 28 digits after the decimal points (128-bit precision).
Next: Operate With Caution >>
More ASP.NET Code Articles
More By Harish Kamath (c) Melonfire