toString参数说明

格式化数值:有时,我们可能需要将数值以一定的格式来呈现,就需要对数值进行格式化。我们使用格式字符串指定格式。格式字符串采用以下形式:Axx,其中 A 为格式说明符,指定格式化类型,xx 为精度说明符,控制格式化输出的有效位数或小数位数。

格式说明符
说明
示例
输出
C
货币
2.5.ToString("C")
¥2.50
D
十进制数
25.ToString("D5")
00025
E
科学型
25000.ToString("E")
2.500000E+005
F
固定点
25.ToString("F2")
25.00
G
常规
2.5.ToString("G")
2.5
N
数字
2500000.ToString("N")
2,500,000.00
X
十六进制
255.ToString("X")
FF
 
public class NumericFormats
{
   public static void Main()
   {
      // Display string representations of numbers for en-us culture
      CultureInfo ci = new CultureInfo( "en-us");

      // Output floating point values

      double floating = 10761.937554; Console.WriteLine("C: {0}",
              floating.ToString("C", ci));           // Displays "C: $10,761.94"
      Console.WriteLine("E: {0}",
              floating.ToString("E03", ci));         // Displays "E: 1.076E+004"
      Console.WriteLine("F: {0}",
              floating.ToString("F04", ci));         // Displays "F: 10761.9376"        
      Console.WriteLine("G: {0}"
              floating.ToString("G", ci));           // Displays "G: 10761.937554"
      Console.WriteLine("N: {0}",
              floating.ToString("N03", ci));         // Displays "N: 10,761.938"
      Console.WriteLine("P: {0}",
              (floating/10000).ToString("P02", ci)); // Displays "P: 107.62 %"
      Console.WriteLine("R: {0}",
              floating.ToString("R", ci));           // Displays "R: 10761.937554"           
      Console.WriteLine();

      // Output integral values
      int integral = 8395;
      Console.WriteLine("C: {0}",
              integral.ToString("C", ci));           // Displays "C: $8,395.00"
      Console.WriteLine("D: {0}",
              integral.ToString("D6", ci));          // Displays D: 008395""
      Console.WriteLine("E: {0}",
              integral.ToString("E03", ci));         // Displays "E: 8.395E+003"
      Console.WriteLine("F: {0}",
              integral.ToString("F01", ci));         // Displays "F: 8395.0"   
      Console.WriteLine("G: {0}"
              integral.ToString("G", ci));           // Displays "G: 8395"
      Console.WriteLine("N: {0}",
              integral.ToString("N01", ci));         // Displays "N: 8,395.0"
      Console.WriteLine("P: {0}",
              (integral/10000).ToString("P02", ci)); // Displays "P: 83.95 %"
      Console.WriteLine("X: 0x{0}",
              integral.ToString("X", ci));           // Displays "X: 0x20CB"
      Console.WriteLine();
   }
}

你可能感兴趣的:(toString)