Another Data Type


In the preceding program, a variable of type int was used. However, an int variable can hold only whole numbers. It cannot be used when a fractional component is required. For example, an int variable can hold the value 18, but not the value 18.3. Fortunately, int is only one of several data types defined by C#. To allow numbers with fractional components, C# defines two floating-point types: float and double, which represent single- and double-precision values, respectively. Of the two, double is the most commonly used.

To declare a variable of type double, use a statement similar to that shown here:

 double result;

Here, result is the name of the variable, which is of type double. Because result has a floating-point type, it can hold values such as 122.23, 0.034, or 19.0.

To better understand the difference between int and double, try the following program:

 /*    This program illustrates the differences    between int and double. */ using System; class Example3 {   public static void Main() {     int ivar;     // this declares an int variable     double dvar;  // this declares a floating-point variable     ivar = 100;   // assign ivar the value 100     dvar = 100.0; // assign dvar the value 100.0     Console.WriteLine("Original value of ivar: " + ivar);     Console.WriteLine("Original value of dvar: " + dvar);     Console.WriteLine(); // print a blank line     // now, divide both by 3     ivar = ivar / 3;     dvar = dvar / 3.0;     Console.WriteLine("ivar after division: " + ivar);     Console.WriteLine("dvar after division: " + dvar);   } }

The output from this program is shown here:

 Original value of ivar: 100 Original value of dvar: 100 ivar after division: 33 dvar after division: 33.3333333333333

As you can see, when ivar is divided by 3, a whole-number division is performed, and the outcome is 33—the fractional component is lost. However, when dvar is divided by 3, the fractional component is preserved.

As the program shows, when you want to specify a floating-point value in a program, it must include a decimal point. If it doesn’t, it will be interpreted as an integer. For example, in C#, the value 100 is an integer, but the value 100.0 is a floating-point value.

There is one other new thing to notice in the program. To print a blank line, simply call WriteLine( ) without any arguments.

The floating-point data types are often used when working with real-world quantities where fractional components are commonly needed. For example, this program computes the area of a circle. It uses the value 3.1416 for pi.

 // Compute the area of a circle. using System; class Circle {   public static void Main() {     double radius;     double area;     radius = 10.0;     area = radius * radius * 3.1416;     Console.WriteLine("Area is " + area);   } }

The output from the program is shown here:

 Area is 314.16

Clearly, the computation of a circle’s area could not be satisfactorily achieved without the use of floating-point data.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net