X3

RedSky

导航

c# 数字转字符串保留两位小数

 

double a = 123456, b = 123456.1, c = 123456.12, d = 123456.123, e = 123456.126;
Console.WriteLine(a.ToString("N"));  //123,456.00
Console.WriteLine(b.ToString("N"));  //123,456.10
Console.WriteLine(c.ToString("N"));  //123,456.12
Console.WriteLine(d.ToString("N"));  //123,456.12
Console.WriteLine(e.ToString("N"));  //123,456.13
Console.WriteLine();

Console.WriteLine(a.ToString("N2"));  //123,456.00
Console.WriteLine(b.ToString("N2"));  //123,456.10
Console.WriteLine(c.ToString("N2"));  //123,456.12
Console.WriteLine(d.ToString("N2"));  //123,456.12
Console.WriteLine(e.ToString("N2"));  //123,456.13
Console.WriteLine();

Console.WriteLine(a.ToString("F2"));  //123456.00
Console.WriteLine(b.ToString("F2"));  //123456.10
Console.WriteLine(c.ToString("F2"));  //123456.12
Console.WriteLine(d.ToString("F2"));  //123456.12
Console.WriteLine(e.ToString("F2"));  //123456.13
Console.WriteLine();

Console.WriteLine(a.ToString("0.00"));  //123456.00
Console.WriteLine(b.ToString("0.00"));  //123456.10
Console.WriteLine(c.ToString("0.00"));  //123456.12
Console.WriteLine(d.ToString("0.00"));  //123456.12
Console.WriteLine(e.ToString("0.00"));  //123456.13
Console.WriteLine();

Console.WriteLine(a.ToString("G"));  //123456
Console.WriteLine(b.ToString("G"));  //123456.1
Console.WriteLine(c.ToString("G"));  //123456.12
Console.WriteLine(d.ToString("G"));  //123456.123
Console.WriteLine(e.ToString("G"));  //123456.126
Console.WriteLine();

//本地货币
Console.WriteLine(a.ToString("C"));  //NT$123,456
Console.WriteLine(b.ToString("C"));  //NT$123,456.1
Console.WriteLine(c.ToString("C"));  //NT$123,456.12
Console.WriteLine(d.ToString("C"));  //NT$123,456.123
Console.WriteLine(e.ToString("C"));  //NT$123,456.126
Console.WriteLine();
Console.Read();

 

posted on 2018-10-20 10:53  HotSky  阅读(14308)  评论(0编辑  收藏  举报