Core C#

Standard Numeric Formatting

This C# program demonstrates how print variables or values via standard numeric formatting in the console window.

Program.cs

using System;

namespace XoaX {
    class Program {
        static void Main(string[] args) {
            string[] saPersons = { "Father", "Son", "Holy Spirit" };

            // Right-justified
            string sFormatted = string.Format("{0,15}", saPersons[0]);
            sFormatted += string.Format("{0,15}", saPersons[1]);
            sFormatted += string.Format("{0,15}", saPersons[2]);
            Console.WriteLine(sFormatted);
            Console.WriteLine();

            // Left-justified
            sFormatted = string.Format("{0,-15}", saPersons[0]);
            sFormatted += string.Format("{0,-15}", saPersons[1]);
            sFormatted += string.Format("{0,-15}", saPersons[2]);
            Console.WriteLine(sFormatted);
            Console.WriteLine();

            // Floating-point formatting
            double dFloatingPoint = 7379.275134;
            // The Unmodified
            sFormatted = string.Format("{0,20}", dFloatingPoint);
            Console.WriteLine(sFormatted);
            // The currency format specifier
            sFormatted = string.Format("{0,20:C2}", dFloatingPoint);
            Console.WriteLine(sFormatted);
            // The exponential format specifier
            sFormatted = string.Format("{0,20:E3}", dFloatingPoint);
            Console.WriteLine(sFormatted);
            // Fixed-point format specifier
            sFormatted = string.Format("{0,20:F4}", dFloatingPoint);
            Console.WriteLine(sFormatted);
            // General format specifier
            sFormatted = string.Format("{0,20:G2}", dFloatingPoint);
            Console.WriteLine(sFormatted);
            // Numeric format specifier
            sFormatted = string.Format("{0,20:N3}", dFloatingPoint);
            Console.WriteLine(sFormatted);
            // Percentage format specifier
            sFormatted = string.Format("{0,20:P3}", dFloatingPoint);
            Console.WriteLine(sFormatted);
            // Round-trip format specifier
            sFormatted = string.Format("{0,20:R}", dFloatingPoint);
            Console.WriteLine(sFormatted);
            Console.WriteLine();

            // Integer formatting
            int dInteger = 737927514;
            // The Unmodified
            sFormatted = string.Format("{0,20}", dInteger);
            Console.WriteLine(sFormatted);
            // The currency format specifier
            sFormatted = string.Format("{0,20:C2}", dInteger);
            Console.WriteLine(sFormatted);
            // The decimal format specifier
            sFormatted = string.Format("{0,20:D4}", dInteger);
            Console.WriteLine(sFormatted);
            // The Exponential format specifier
            sFormatted = string.Format("{0,20:E4}", dInteger);
            Console.WriteLine(sFormatted);
            // The Fixed-point format specifier
            sFormatted = string.Format("{0,20:F3}", dInteger);
            Console.WriteLine(sFormatted);
            // The General format specifier
            sFormatted = string.Format("{0,20:G2}", dInteger);
            Console.WriteLine(sFormatted);
            // The Numeric format specifier
            sFormatted = string.Format("{0,20:N4}", dInteger);
            Console.WriteLine(sFormatted);
            // The Percentage format specifier
            sFormatted = string.Format("{0,20:P3}", dInteger);
            Console.WriteLine(sFormatted);
            // The Hexadecimal format specifier
            sFormatted = string.Format("{0,20:X}", dInteger);
            Console.WriteLine(sFormatted);
            Console.WriteLine();
        }
    }
}
 

Output

         Father            Son    Holy Spirit

Father         Son            Holy Spirit

         7379.275134
           $7,379.28
          7.379E+003
           7379.2751
             7.4E+03
           7,379.275
        737,927.513%
         7379.275134

           737927514
     $737,927,514.00
           737927514
         7.3793E+008
       737927514.000
             7.4E+08
    737,927,514.0000
 73,792,751,400.000%
            2BFBE15A

Press any key to continue . . .
 
 

© 2007–2024 XoaX.net LLC. All rights reserved.