Home

Using Variables: Real Numbers

 

Introduction

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.

Floating-Point Numbers

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;
    }
}

Double-Precision Numbers

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

Practical LearningPractical Learning: Using a Double-Precision Variable

  1. To use a double-precision value, change the file as follows:
     
    using System;
    
    class Program
    {
        static void Main()
        {
            byte shirts;
            byte pants;
            ushort otherItems;
    
            uint orderDay;
            uint orderMonth;
            uint orderYear;
    
            double mondayDiscount;
    
            shirts     = 4;
            pants      = 0;
            otherItems = 3;
            orderDay = 15;
            orderMonth = 7;
            orderYear = 2002;
            mondayDiscount = 0.25; // 25%
    
            Console.WriteLine("-/- Georgetown Cleaning Services -/-");
            Console.WriteLine("========================");
            Console.Write("Order Date: ");
            Console.Write(orderMonth);
            Console.Write('/');
            Console.Write(orderDay);
            Console.Write('/');
            Console.WriteLine(orderYear);
            Console.WriteLine("------------------------");
            Console.WriteLine("Item Type  Qty");
            Console.WriteLine("------------------------");
            Console.Write("Shirts      ");
            Console.WriteLine(shirts);
            Console.Write("Pants       ");
            Console.WriteLine(pants);
            Console.Write("Other Items ");
            Console.WriteLine(otherItems);
            Console.WriteLine("------------------------");
            Console.Write("Monday Discount: ");
            Console.Write(mondayDiscount);
            Console.WriteLine('%');
            Console.WriteLine("========================");
            Console.WriteLine();
        }
    }
  2. Execute the application to see the result:
     
    -/- Georgetown Cleaning Services -/-
    ========================
    Order Date: 7/15/2002
    ------------------------
    Item Type  Qty
    ------------------------
    Shirts      4
    Pants       0
    Other Items 3
    ------------------------
    Monday Discount: 0.25%
    ========================
    
    Press any key to continue . . .
  3. Close the DOS window

Decimal

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

Practical LearningPractical Learning: Using Decimal Values

  1. To use decimal variables, change the file as follows:
     
    using System;
    
    class Program
    {
        static void Main()
        {
            byte    shirts;
            decimal priceOneShirt;
    
            byte    pants;
            decimal priceAPairOfPants;
    
            ushort  otherItems;
            decimal priceOtherItems;
    
            uint orderDay;
            uint orderMonth;
            uint orderYear;
    
            double mondayDiscount;
    
            shirts = 5;
            priceOneShirt = 0.95M;
            pants = 2;
            priceAPairOfPants = 1.95M;
            otherItems = 3;
            priceOtherItems = 4.55M;
            orderDay = 15;
            orderMonth = 7;
            orderYear = 2002;
            mondayDiscount = 0.25; // 25%
    
            Console.WriteLine("-/- Georgetown Cleaning Services -/-");
            Console.WriteLine("========================");
            Console.Write("Order Date: ");
            Console.Write(orderMonth);
            Console.Write('/');
            Console.Write(orderDay);
            Console.Write('/');
            Console.WriteLine(orderYear);
            Console.WriteLine("------------------------");
            Console.WriteLine("Item Type  Qty Unit Price");
            Console.WriteLine("------------------------");
            Console.Write("Shirts      ");
            Console.Write(shirts);
            Console.Write("     ");
            Console.WriteLine(priceOneShirt);
            Console.Write("Pants       ");
            Console.Write(pants);
            Console.Write("     ");
            Console.WriteLine(priceAPairOfPants);
            Console.Write("Other Items ");
            Console.Write(otherItems);
            Console.Write("     ");
            Console.WriteLine(priceOtherItems);
            Console.WriteLine("------------------------");
            Console.Write("Monday Discount: ");
            Console.Write(mondayDiscount);
            Console.WriteLine('%');
            Console.WriteLine("========================");
            Console.WriteLine();
        }
    }
  2. Execute the program. This would produce:
     
    -/- Georgetown Cleaning Services -/-
    ========================
    Order Date: 7/15/2002
    ------------------------
    Item Type  Qty Unit Price
    ------------------------
    Shirts      5     0.95
    Pants       2     1.95
    Other Items 3     4.55
    ------------------------
    Monday Discount: 0.25%
    ========================
    
    Press any key to continue . . .
  3. Close the DOS window
 

Previous Copyright © 2006-2007 FunctionX, Inc. Next