Format: {0,-10}
using System;
class MainClass
{
static void Main()
{
double MyDouble = 12.345678;
Console.WriteLine("{0,-10} -- Default, same as General", MyDouble);
}
}
12.345678 -- Default, same as General
Format: {0,-10:C} -- Currency
using System;
class MainClass
{
static void Main()
{
double MyDouble = 12.345678;
Console.WriteLine("{0,-10:C} -- Currency", MyDouble);
}
}
$12.35 -- Currency
Format: {0,-10:E3} -- Sci. Notation, 3 dec places
using System;
class MainClass
{
static void Main()
{
double MyDouble = 12.345678;
Console.WriteLine("{0,-10:E3} -- Sci. Notation, 3 dec places", MyDouble);
}
}
1.235E+001 -- Sci. Notation, 3 dec places
Format: {0,-10:F4} -- Fixed Point, 4 decimal places
using System;
class MainClass
{
static void Main()
{
double MyDouble = 12.345678;
Console.WriteLine("{0,-10:F4} -- Fixed Point, 4 decimal places", MyDouble);
}
}
12.3457 -- Fixed Point, 4 decimal places
Format: {0,-10:G}
using System;
class MainClass
{
static void Main()
{
double MyDouble = 12.345678;
Console.WriteLine("{0,-10:G} -- General", MyDouble);
}
}
12.345678 -- General
Format: {0,-10:x} -- Hexadecimal integer
using System;
class MainClass
{
static void Main()
{
Console.WriteLine("{0,-10:x} -- Hexadecimal integer", 1194719);
}
}
123adf -- Hexadecimal integer
Format: Allignment
using System;
class MainClass
{
static void Main()
{
Console.WriteLine("|{0, 10}|", 999); // Aligned right
Console.WriteLine("|{0,-10}|", 000); // Aligned left
}
}
| 999|
|0 |
Format: Currency
using System;
class MainClass
{
static void Main()
{
Console.WriteLine("The value: {0}.", 500); // Print out number.
Console.WriteLine("The value: {0:C}.", 500); // Format as currency.
}
}
The value: 500.
The value: $500.00.
Format double value
using System;
class Example {
public static void Main() {
double s1 = 1231234.123412341234;
Console.WriteLine("{0:#.###}.", s1);
}
}
1231234.123.
Numeric formatting specifiers
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
Use format commands: indicate the parameter sequence
using System;
class Example {
public static void Main() {
int i;
Console.WriteLine("Value\tSquared\tCubed");
for(i = 1; i < 10; i++)
Console.WriteLine("{0}\t{1}\t{2}",
i, i*i, i*i*i);
}
}
Value Squared Cubed
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
Use the C format specifier to output dollars and cents.
using System;
class Example {
public static void Main() {
decimal price;
decimal discount;
decimal discounted_price;
price = 19.95m;
discount = 0.15m;
discounted_price = price - ( price * discount);
Console.WriteLine("Discounted price: {0:C}", discounted_price);
}
}
Discounted price: $16.96