CSharp - Write program to Format Numbers

Requirements

You will use the ToString method to get nicely formatted numeric output.

Control the decimal places, rounding, thousands separation, and so on.

Hint

In the format strings, N means thousands separation is required, and N2 and N0 denote the number of decimal places in the output.

Demo

using System;

class Program// w w  w  . j  a va2  s .c om
{
    static void Main(string[] args)
    {
        // Some money amounts and a number 
        double amount = 1234.56;
        double anotherAmount = 789;
        int wholeNumber = 1234567;

        // Formatted outputs 
        Console.WriteLine(amount.ToString("N2"));
        Console.WriteLine(anotherAmount.ToString("N2"));
        Console.WriteLine(wholeNumber.ToString("N0"));
    }
}

Result