Numeric formatting specifiers : Console Output Format « Development « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;

public class MainClass
{
    public static void Main()
    {
        Console.WriteLine("C: {0}", 39.22M.ToString("C"));
        Console.WriteLine("D: {0}", 982L.ToString("D"));
        Console.WriteLine("E: {0}", 3399283712.382387D.ToString("E"));
        Console.WriteLine("F: {0}", .993F.ToString("F"));
        Console.WriteLine("G: {0}", 32.559D.ToString("G"));
        Console.WriteLine("N: {0}", 93823713.ToString("N"));
        Console.WriteLine("P: {0}", .59837.ToString("P"));
        Console.WriteLine("R: {0}", 99.33234D.ToString("R"));
        Console.WriteLine("X: {0}", 369329.ToString("X"));
    }
}
C: $39.22
D: 982
E: 3.399284E+009
F: 0.99
G: 32.559
N: 93,823,713.00
P: 59.84 %
R: 99.33234
X: 5A2B1








14.4.Console Output Format
14.4.1.Format: Allignment
14.4.2.Format: Currency
14.4.3.Format: {0,-10:G}
14.4.4.Format: {0,-10}
14.4.5.Format: {0,-10:F4} -- Fixed Point, 4 decimal places
14.4.6.Format: {0,-10:C} -- Currency
14.4.7.Format: {0,-10:E3} -- Sci. Notation, 3 dec places
14.4.8.Format: {0,-10:x} -- Hexadecimal integer
14.4.9.Use format commands: indicate the parameter sequence
14.4.10.Use the C format specifier to output dollars and cents.
14.4.11.Format double value
14.4.12.Numeric formatting specifiers
14.4.13.Display 4 mandatory and 3 optional arguments using the Console.WriteLine method.