C# Tutorial - Application 8 – Converting Decimal Numbers


Create a new console Application called App08. We are going to modify the application so that we can enter decimal point numbers into the calculator. Copy the code from App07 and modify value1 and value2 so that they are of type double.

Now we need to change the GetNumber() method so that it gets a decimal point number from the user instead of an integer.

private static double GetNumber()
{
  double d = 0;
  first:
  try
  {
    Console.Write("Enter a number: ");
    d = System.Convert.ToDouble(Console.ReadLine());
  }
  catch (System.FormatException)
  {
    Console.WriteLine("String does not consist of an " +
        "optional sign followed by a series of numbers.");
    goto first;
  }
  catch (System.OverflowException)
  {
    Console.WriteLine("Value is out of range.");
    goto first;
  }

  return d;
}

We have used another method of the Convert class ToDouble(), which returns a decimal point number. There are only two exceptions according to MSDN that this will throw, and so they have been handled.

A final change to the application is to remove the cast form the division case of the switch statement.

// Perform the operation
switch( operation )
{
  case "+": total = value1 + value2; break;
  case "-": total = value1 - value2; break;
  case "/": total = value1 / value2; break;
  case "*": total = value1 * value2; break;
  default: /* Ignore other keypresses */ break;
}

One error that you may wish to fix now is the application does not do anything useful when an invalid operation is entered. This should allow you to try the skills out that you have learned so far.

Casting Data Types

In App07, the division case of the switch statement contains a data type within a set of brackets preceding a variable. This is known as casting a variable to a different data type.

case "/": total = (double)value1 / (double)value2; break;

What this is doing is telling the compiler to ignore the data type of the variable and to treat it as though it is of the data type that appears between the brackets. So in real terms we are converting the data type from one type to another.

The next piece of code demonstrates two ways of achieving the same result.

int value1 = 3;
double total;

// First way of converting is by casting the value.
total = (double)value1;

// Second way is to use the Convert class method.
total = Convert.ToDouble(value1);

A Word of Warning

If you are not careful, you can lose valuable information held in the variable if you wrongly cast the variable to another type which holds less information.

For example, casting an int to a short would be an unwise thing to do as an int holds 32 bits of data, while a short only holds 16 bits of data.

A good rule of thumb is to only cast a value up into a larger data type and not down. The following table lists some safe casts.

Type Can Be Safely Converted To
byte char, short, int, long, float, double, decimal
short int, long, float, double, decimal
int long, float, double, decimal
long float, double, decimal
float double, decimal
double decimal

Final Note

This does not stop you from casting a larger type to a smaller one, as sometimes this will be exactly what you want to do as you may only be interested in part of the value.


<< Previous Contents Next >>

© Publicjoe, 2008