1. 程式人生 > >C#數值筆記一

C#數值筆記一

logs tel col ima mat string 分享 als decimal

好記性不如爛筆頭~~

關於數值的筆記由此開始~~

 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 }

技術分享

C#數值筆記一