Monday, January 23, 2012

3.2.3. Formatting Numbers

We might be interested in formatting numbers, so that they show a certain amount of decimal places. For example, we may want a money expression to be always displayed with two decimal places, or a mark to be displayed rounded, with no decimal places.

We can achieve it using "ToString". This command can receive additional data to indicate the format we want to use, for example: sum.ToString ("0.00")

Some of the formatting codes we can use are:

·       A zero (0) indicates a position where you should see a digit, and displays a 0 if there is none.
·       A hash (#) indicates a position where you can see a digit, but nothing is written if there is no digit.
·       A period (.) Indicates the position for the decimal point.
·       Alternatively, you can use other abbreviated formats. For instance, N2 means "to two decimal places" and N5 is "to five decimal places"

Let's try them in an example:

/*---------------------------*/
/*  C# Example #28:          */
/*  example28.cs             */
/*                           */
/*  Real numbers formatting  */
/*                           */
/*  Intro to C#,             */
/*    Nacho Cabanes          */
/*---------------------------*/

using System;

public class Example28
{
    public static void Main()
    {
        double number = 12.34;
       
        Console.WriteLine( number.ToString("N1") );
        Console.WriteLine( number.ToString("N3") );
        Console.WriteLine( number.ToString("0.0") );
        Console.WriteLine( number.ToString("0.000") );
        Console.WriteLine( number.ToString("#.#") );
        Console.WriteLine( number.ToString("#.###") );
    }
}

The result of this example is:

12,3
12,340
12,3
12,340
12,3
12,34

As you can see, the following happens:
·       If we indicate fewer decimal places than the number has, the number is rounded.
·       If we ask for more decimal places than the decimal digits the number has, zeros will we displayed if we use a format as Nx or 0,000, and nothing will be displayed if we use #.###
·       If we indicate less digits before the decimal point that what the number really has, all of them will still be displayed.


Suggested exercises:

Ø     Ask the user for two numbers up to 12 significant digits. The program should display the result of dividing the first number by the second, using three decimal places.
Ø     Create a program that uses three variables x, y, z. All of them must be real numbers, with two decimal places. You should ask the user for values for those three variables and display the value of x2 + y - z (using exactly two decimal places).
Ø     Calculate the perimeter, area and diagonal of a rectangle from its width and height (perimeter = sum of the four sides, area = base x height, diagonal using the Pythagorean theorem). Show all of them with one decimal place.
Ø     Calculate the area and volume of a sphere from its radius (area = 4 * pi * radius squared, volume = 4/3 * pi * radius cubed). Show the results with 3 decimal places.

No comments:

Post a Comment