c#小Tip:数字格式化显示

None.gifusing System;
None.gif
None.gifclass FormattingNumbers
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif  static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif    decimal theDecNumber = 12345.678m; //the "m" creates a literal of type decimal from a double
InBlock.gif    //Using the ToString Method
InBlock.gif    //the number in the format string is the precision specifier
InBlock.gif    Console.WriteLine("No formatting: " + theDecNumber.ToString());
InBlock.gif    Console.WriteLine("Currency formatting: " + theDecNumber.ToString("C"));
InBlock.gif    Console.WriteLine("Exponential formatting: " + theDecNumber.ToString("E"));
InBlock.gif    Console.WriteLine("Fixed-point formatting: " + theDecNumber.ToString("F2"));
InBlock.gif    Console.WriteLine("General formatting: " + theDecNumber.ToString("G"));
InBlock.gif    Console.WriteLine("Number formatting to 2 decimal places: " + theDecNumber.ToString("N2"));
InBlock.gif    Console.WriteLine("Number formatting to 3 decimal places: " + theDecNumber.ToString("N3"));
InBlock.gif    Console.WriteLine("Number formatting to 4 decimal places: " + theDecNumber.ToString("N4"));
InBlock.gif    Console.WriteLine("Percent formatting: " + theDecNumber.ToString("P0"));
InBlock.gif
InBlock.gif    int theIntNumber = 123456;
InBlock.gif    Console.WriteLine("Hexidecimal formatting (for integers): {0} = {1}", theIntNumber, theIntNumber.ToString("X"));
InBlock.gif
InBlock.gif    double theDblNumber = 1234567890;
InBlock.gif    Console.WriteLine("Custom formatting: {0} to US telephone {1}", theDblNumber, theDblNumber.ToString( "(###) ### - ####" ));
InBlock.gif
InBlock.gif    //Keep console open if not run through command prompt
InBlock.gif    Console.Write("\nPress Enter to Continue");
InBlock.gif    Console.ReadLine();
ExpandedSubBlockEnd.gif  }
ExpandedBlockEnd.gif}
None.gif