好记性不如烂笔头~~
关于数值的笔记由此开始~~
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 var integer = 10000; 6 var decimals = 10000.12345; 7 var percent = 0.12345; 8 9 //四舍五入保留N位小数,默认保留两位小数 10 Console.WriteLine("{0}.ToString(\"0\") = {1}".FormatWith(integer, integer.ToString("0.00"))); //10000.00 11 Console.WriteLine("{0}.ToString(\"0.00\") = {1}".FormatWith(integer, integer.ToString("0.00"))); //10000.00 12 Console.WriteLine("{0}.ToString(\"0.00\") = {1}".FormatWith(decimals, decimals.ToString("0.00"))); //10000.12 13 Console.WriteLine("{0}.ToString(\"0.00000\") = {1}".FormatWith(decimals, decimals.ToString("0.0000"))); //10000.1235 14 Console.WriteLine(); 15 16 //四舍五入保留N位小数,默认保留两位小数 17 Console.WriteLine("{0}.ToString(\"F\") = {1}".FormatWith(integer, integer.ToString("F"))); //10000.00 18 Console.WriteLine("{0}.ToString(\"F2\") = {1}".FormatWith(integer, integer.ToString("F2"))); //10000.00 19 Console.WriteLine("{0}.ToString(\"F2\") = {1}".FormatWith(decimals, decimals.ToString("F2"))); //10000.12 20 Console.WriteLine("{0}.ToString(\"F4\") = {1}".FormatWith(decimals, decimals.ToString("F4"))); //10000.1235 21 Console.WriteLine(); 22 23 //以千分位分隔数字,四舍五入保留N位小数,默认保留两位小数 24 Console.WriteLine("{0}.ToString(\"N\") = {1}".FormatWith(integer, integer.ToString("N"))); //10,000.00 25 Console.WriteLine("{0}.ToString(\"N2\") = {1}".FormatWith(integer, integer.ToString("N2"))); //10,000.00 26 Console.WriteLine("{0}.ToString(\"N2\") = {1}".FormatWith(decimals, decimals.ToString("N2"))); //10,000.12 27 Console.WriteLine("{0}.ToString(\"N4\") = {1}".FormatWith(decimals, decimals.ToString("N4"))); //10,000.1235 28 29 //以百分比表示数字,四舍五入保留N位小数,默认保留两位小数 30 Console.WriteLine("{0}.ToString(\"P\") = {1}".FormatWith(percent, percent.ToString("P"))); //12.35% 31 Console.WriteLine("{0}.ToString(\"P1\") = {1}".FormatWith(percent, percent.ToString("P1"))); //12.3% 32 Console.WriteLine("{0}.ToString(\"P2\") = {1}".FormatWith(percent, percent.ToString("P2"))); //12.35% 33 34 Console.ReadKey(); 35 } 36 }
时间: 2024-12-07 14:25:05