Format input numbers using a variety of format specifiers input at run time. - CSharp Language Basics

CSharp examples for Language Basics:Data Type Format

Description

Format input numbers using a variety of format specifiers input at run time.

Demo Code

using System;//from w  w w. j a v a2 s.c o m
public class Program
{
   public static void Main(string[] args)
   {
      for(;;)
      {
         Console.WriteLine("Enter a double number");
         string numberInput = Console.ReadLine();
         if (numberInput.Length == 0)
         {
            break;
         }
         double number = Double.Parse(numberInput);
         Console.WriteLine("Enter the format specifiers"+ " separated by a blank " + "(Example: C E F1 N0 0000000.00000)");
         char[] separator = {' '};
         string formatString = Console.ReadLine();
         string[] formats = formatString.Split(separator);
         foreach(string s in formats)
         {
            if (s.Length != 0)
            {
               string formatCommand = "{0:" + s + "}";
               Console.Write("The format specifier {0} results in ", formatCommand);
               try
               {
                  Console.WriteLine(formatCommand, number);
               }
               catch(Exception)
               {
                  Console.WriteLine("<illegal control>");
               }
            }
         }
      }
   }
}

Result


Related Tutorials