• 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

  • 相关阅读:
    KafkaSpout 重复消费问题解决
    FastJson 输出值 首字母大小写问题
    Kafka0.7运行时报错 kafka/javaapi/consumer/ConsumerConnector : Unsupported major.minor version 51.0 解决
    Zookeeper原理与Curator使用
    Strom 消息处理机制 中英对照翻译 (Storm如何保证消息被完全处理)
    Mac安装 Storm 小结
    linux下实现ftp上传文件
    Task 0.0 in stage 1.0 (TID 1) had a not serializable result: org.apache.hadoop.hbase.client.Result
    Spark操作HBase
    maven-pom-project文件报错
  • 原文地址:https://www.cnblogs.com/haibing0107/p/5796320.html
Copyright © 2020-2023  润新知