C# tutorial-Output format

Format output in C#

You can format an item to be output to the screen by using the following syntax:


The index component is a number starting from 0 that identifies a corresponding item in the list of items. The alignment component is an integral number representing the width of the item. If the alignment is less than zero, the item will be aligned left, and right aligned if it is greater than zero. This number should be greater than the length of the formatted item, otherwise, it will be ignored. The formatString component is a type of format string that is appropriate for the item being formatted. For example, to format a number to a string of currency, use C or c; to decimal number, use D or d; to percentage, use P or p. To format a date in the form of  “dd-mm-yy”, your format string can be “dd-mm-yy”.

Console.WriteLine("Sub total: {0,8:c}", subtotal);

Console.WriteLine("Discounted total: {0,8:c}", subTaxable);

DateTime d = DateTime.Now;

Console.WriteLine("{0:dd-mm-yy}", d);


Example: calculate a sale given the unit price per item, quantity of items sold, discount rate, and sale tax rates.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace Ctest


    class Program



       //global variables should be declared here.


        static void Main(string[] args)


        //local variables

            float tax_rate = 0.09F;

            float unitprice;

            int quantity;

            float subtotal;

            float discountAm;

            float subTaxable;

            float discountRate;

            float taxAm;

            float total;

            /* statements */

            Console.WriteLine("Enter quantity:");

            quantity = int.Parse(Console.ReadLine());      

            Console.WriteLine("Enter unit price:");

            unitprice = float.Parse(Console.ReadLine());

            Console.WriteLine("Enter discount rate:");

            discountRate = float.Parse(Console.ReadLine());


            subtotal = quantity * unitprice;

            discountAm = subtotal * discountRate/100.0F;

            subTaxable = subtotal - discountAm;

            taxAm = subTaxable * tax_rate;

            total = subTaxable + taxAm;

            Console.WriteLine("Quantity sold:{0,11}",quantity);

            Console.WriteLine("Unit price:{0,14:c}", unitprice);


            Console.WriteLine("Sub total:{0,15:c}", subtotal);

            Console.WriteLine("Discounted total:{0,8:c}", subTaxable);

            Console.WriteLine("Sales tax:{0,15:c}",taxAm);

            Console.WriteLine("Total:{0,19:c}", total);









Muna comment



KrishnaMohan comment


Check this also for C# interview questions

oko comment



Abduraafi comment


Hai, it realy useful


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.