Format a numeric string and a string
Syntax of format specifiers
Alignment specifier
A positive value indicates the right alignment, and a negative value indicates the left alignment. If the number of characters to be expressed is less than the number specified in the alignment specifier, spaces are used. If the number is more, the characters are ignored.
1 static void Main(string[] args)2 {3 var temp = 1000;4 Console.WriteLine("|{0,10}|", temp);5 Console.WriteLine("|{0,-10}|", temp);6 Console.WriteLine("|{0,3}|", temp);7 }
Format Field
1 var temp = 123.456789; 2 Console. writeLine ("{0: F3}", temp); // fixed point, retain the number of decimal places 3 Console. writeLine ("{0: C}", temp); // indicates the currency, depending on the PC region set 4 Console. writeLine ("{0: D10}", 123); // Console in decimal number 5. writeLine ("{0: G4}", temp); // retain the "number length" according to the description, and the last digit is rounded to 6 consoles. writeLine ("{0: X}", 123); // convert to hexadecimal (case sensitive) 7 Console. writeLine ("{0: N4}", 1234567,890123); // separate the numbers by commas. writeLine ("{0: P}", temp); // percentage 9 Console. writeLine ("{0: R}", temp); // 10 Console. writeLine ("{0: E3}", temp); // scientific notation (case sensitive)