zoukankan      html  css  js  c++  java
  • C# string.Format()格式

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 
      6 namespace ConsoleApplication1
      7 {
      8     class Program
      9     {
     10         static void Main(string[] args)
     11         {
     12             #region 数字格式化
     13             //货币
     14             Format_C();
     15 
     16             //十进制
     17             Format_D();
     18 
     19             //科学计算法
     20             Format_E();
     21 
     22             //常规
     23             Format_G();
     24 
     25             //用逗号隔开数字
     26             Format_N();
     27 
     28             //16进制
     29             Format_X();
     30 
     31             //格式化数字
     32             Format_F();
     33 
     34             //格式化百分比
     35             Format_P();
     36 
     37             #endregion
     38 
     39             Console.ReadKey();
     40         }
     41 
     42 
     43         #region 数字格式化
     44 
     45         /// <summary>
     46         /// 货币('C'后面跟的数字如果是 '2' 得到的结果‘¥2.00’,所以C 后面跟的数就是小数点后面的数 )
     47         /// </summary>
     48         public static void Format_C()
     49         {
     50             Console.WriteLine("货币:"+string.Format("{0:C2}", 2));
     51         }
     52 
     53 
     54         /// <summary>
     55         /// 十进制(如果是 'D2'则结果是 02 )
     56         /// </summary>
     57         private static void Format_D()
     58         {
     59             Console.WriteLine("十进制:"+string.Format("{0:D3}", 2));
     60         }
     61 
     62         /// <summary>
     63         /// 科学计数法
     64         /// </summary>
     65         private static void Format_E()
     66         {
     67             Console.WriteLine(string.Format("科学计数法:" + "{0:E2}", 100000));
     68         }
     69 
     70         /// <summary>
     71         /// 常规
     72         /// </summary>
     73         private static void Format_G()
     74         {
     75             Console.WriteLine(string.Format("常规:" + "{0:G}", 100000));
     76         }
     77 
     78         /// <summary>
     79         /// 用逗号隔开数字(如果N后面跟的是1,小数点后面则跟着一个0)
     80         /// </summary>
     81         private static void Format_N()
     82         {
     83             Console.WriteLine(string.Format("用逗号隔开:" + "{0:N1}", 100000));
     84         }
     85 
     86         /// <summary>
     87         /// 16进制
     88         /// </summary>
     89         private static void Format_X()
     90         {
     91             Console.WriteLine(string.Format("16进制:" + "{0:X}", 10));
     92         }
     93 
     94 
     95         /// <summary>
     96         /// 格式化数字(如果是 'F3'则值是 10.098)
     97         /// </summary>
     98         private static void Format_F()
     99         {
    100             Console.WriteLine(string.Format("固定值:" + "{0:F2}", 10.098));
    101         }
    102 
    103         /// <summary>
    104         /// 格式化百分比(如果是 'P2' 则值是82.00%)
    105         /// </summary>
    106         private static void Format_P()
    107         {
    108             Console.WriteLine(string.Format("百分比:" + "{0:P0}", 0.82));
    109         }
    110 
    111         #endregion
    112 
    113 
    114     }
    115 }
    View Code

  • 相关阅读:
    HDU 2878 Great World of Goo
    HDU 4295 4 substrings problem
    UVALive 4975 Casting Spells
    UVA 10572 Black & White
    POJ 3133 Manhattan Wiring
    HDU 4267 A Simple Problem with Integers
    SPOJ 7258 Lexicographical Substring Search
    UVALive 4978 Fields and Farmers
    ssh连接aix问题与解决
    MDRT_<>$表
  • 原文地址:https://www.cnblogs.com/haibing0107/p/5796320.html
Copyright © 2011-2022 走看看