Sponsored Ad

Monday, May 3, 2010

Number and Currency formatting in C#

 

The formats are given below along with example, please let me know if you face any problem.

 

Output:

Number and Currency formatting in C#

 

Source code:

using System;
using System.Collections.Generic;
using System.Text;

namespace MyFirstConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int intResults = 1234567890;
            double dblResults = 12345678.90;

            Console.WriteLine("Currency format {0}: {1:C}", "{C}", intResults);

            Console.WriteLine("Decimal format {0}: {1:D}", "{D}", intResults);

            Console.WriteLine("Decimal format {0}: {1:D12}", "{D12}", intResults);

            Console.WriteLine("Exponential format {0}: {1:E}", "{E}", intResults);

            Console.WriteLine("fixed format {0}: {1:F}", "{F}", intResults);

            Console.WriteLine("fixed format {0}: {1:F5}", "{F5}", intResults);

            Console.WriteLine("number format {0}: {1:N}", "{N}", dblResults);

            Console.WriteLine("number format {0}: {1:N1}", "{N1}", dblResults);

            Console.WriteLine("hexadecimal format {0}: {1:X}", "{X}", intResults);

            Console.WriteLine(" format {0}: {1:0.000}", "{0.000}", intResults);

            Console.WriteLine(" format {0}: {1:0.##}", "{0.##}", intResults);

            Console.WriteLine("percent format {0}: {1:0%}", "{0%}", intResults);

            Console.ReadLine();
        }

    }
}

No comments:

Post a Comment

Sponsored Ad

Development Updates