Wednesday, 13 October 2010

How to Print a Number in Decimal using C#

Steps to develop a program :

1. Use {0:d} to print decimal number.

2. Either use with String.Format() or WriteLine().

Decimal number Printing in C#:

using System;
using System.Text;

namespace TestConsole
{
    class DecimalFormat
    {
        static void Main(string[] args)
        {
            Console.WriteLine(String.Format("{0:d}", 254));
            Console.WriteLine(String.Format("{0:d}", -783));
            //error
            //Console.WriteLine(String.Format("{0:d}", 193.0));
            //or
            Console.WriteLine("{0:d}", 1000);

            Console.ReadLine();
        }
    }
}

Output:

How to Print a Number in Decimal using C#

No comments:

Post a Comment