Using Variables: Real Numbers |
|
A real number is a number that displays a decimal part. This means that the number can be made of two sections separated by a symbol that is referred to as the Decimal Separator or Decimal Symbol. This symbol is different by language, country, group of languages, or group of countries. In US English, this symbol is the period as can be verified from the Regional (and Language) Settings of the Control Panel: |
On both sides of the Decimal Symbol, digits are used to specify the value of the number. The number of digits on the right side of the symbol determines how much precision the number offers. The integers we have used so far have the main limitation of not allowing decimal values. C# provides floating values that would solve this problem. The most fundamental floating variable is declared with the float keyword. A variable declared a float can store real numbers that range from ±1.5 × 10−45 to ±3.4 × 1038 with a precision of 7 digits in 32 bits. Here is an example: using System; class NumericRepresentation { static void Main() { float Distance; } } When a variable is larger than the float can handle and requires more precision, you should declare it using the double keyword. A variable declared as double uses 64 bits to store very large numbers ranging from ±5.0 × 10−324 to ±1.7 × 10308 with a precision of 15 or 16 digits. Here is an example: using System; class NumericRepresentation { static void Main() { double StudentAge; StudentAge = 14.50; Console.Write("Student Age: "); Console.Write(StudentAge); Console.WriteLine(); } } This would produce: Student Age: 14.5 Because the double data type provides a better result with a better precision than the float, whenever you declare a variable as float and assign it a value as we did earlier, the compiler allocates 64 bits to store the values of the variable. If you insist on the variable being treated as float, when assigning it a value, add an F suffix to the value. Here is an example: using System; class NumericRepresentation { static void Main() { float Distance; Distance = 248.38F; Console.Write("Distance = "); Console.Write(Distance); Console.WriteLine("km\n"); } } This would produce: Distance = 248.38km
The decimal data type can be used to declare a variable that would hold significantly large values that can be stored in a combination of 128 bits. You declare such a variable using the decimal keyword. The values stored in a decimal variable can range from ±1.0 × 10−28 to ±7.9 × 1028 with a precision of 28 to 29 digits. Because of this high level of precision, the decimal data type is suitable for currency values. After declaring a decimal variable, you can initialize it with a natural number. To indicate that the variable holds a decimal value, when initializing it, add an M suffix to its value. Here is an example: using System; class NumericRepresentation { static void Main() { decimal HourlySalary; HourlySalary = 24; Console.Write("Hourly Salary = "); Console.WriteLine(HourlySalary); Console.WriteLine(); } } This would produce: Hourly Salary = 24
|
|
||
Previous | Copyright © 2006-2007 FunctionX, Inc. | Next |
|