c# ToString格式化

在做类对象自定义比对的时候,从数据库中取出的数据和本地生成的数据使用默认的ToString()输出未必一直,如下:

//2018-01-29
//sql--date--2018-01-29 00:00:00.123
//2018-01-29 00:00:00

//1m
//sql--numeric(18, 2)--1.00
//1

数字转换为String,使用Math.Round保留小数位数:

public void ConvertToString()
{
    //float--f
    var a = 1f;
    Console.WriteLine(Math.Round(a, 4));//1
    a = 1.12f;
    Console.WriteLine(Math.Round(a, 4));//1.12
    a = 1.12345f;
    Console.WriteLine(Math.Round(a, 4));//1.1235

    //decimal--m,double--d
    var b = 1m;//1d;
    Console.WriteLine(Math.Round(b, 4));//1
    b = 1.12m;//1/12d;
    Console.WriteLine(Math.Round(b, 4));//1.12
    b = 1.12345m;//1.12345d
    Console.WriteLine(Math.Round(b, 4));//1.1234,不是四舍五入,而是四舍六入五取偶
}

第一个问题就是数字本身小数点,第二个问题就是小数值四舍五入的算法,微软的Math.Round并不是传统的中国四舍五入,而是四舍六入五取偶,即0.1235==0.124,0.1245==0.124。

Math.Round有8中重载,如下:

Round(Decimal)
Round(Double)
Round(Decimal, Int32)
Round(Decimal, MidpointRounding)
Round(Double, Int32)
Round(Double, MidpointRounding)
Round(Decimal, Int32, MidpointRounding)//仅在这设置为MidpointRounding.AwayFromZero,能保证是四舍五入
Round(Double, Int32, MidpointRounding)

只有在Round(Decimal, Int32, MidpointRounding=MidpointRounding.AwayFromZero)时是四舍五入。

如下测试:

using System;
 
public class Test
{
    public static void Main()
    {
        Console.WriteLine(Math.Round(0.31249999m,3,MidpointRounding.AwayFromZero));//0.312

        Console.WriteLine(Math.Round(0.31249999,3,MidpointRounding.AwayFromZero));//0.312
Console.WriteLine(Math.Round(0.31249999f,3,MidpointRounding.AwayFromZero));//0.313 Console.ReadKey(); } }

考虑使用ToString格式化字符串,整理如下:

1.Guid

var guid = Guid.NewGuid();
//D,N,B,P大写小写效果是一样
Console.WriteLine(guid.ToString("D"));// 10244798-9a34-4245-b1ef-9143f9b1e68a
Console.WriteLine(guid.ToString("N"));// 102447989a344245b1ef9143f9b1e68a
Console.WriteLine(guid.ToString("B"));// {10244798-9a34-4245-b1ef-9143f9b1e68a}
Console.WriteLine(guid.ToString("P"));// (10244798-9a34-4245-b1ef-9143f9b1e68a)
Console.WriteLine(guid.ToString());// 10244798-9a34-4245-b1ef-9143f9b1e68a

2.Date

DateTime dt = new DateTime(2018, 1, 1, 12, 5, 5, 123);//2018-01-01 05:06:07.123
Console.WriteLine(dt.ToString("y"));//2018年1月 
Console.WriteLine(dt.ToString("Y"));//2018年1月 
Console.WriteLine(dt.ToString("m"));//1月1日
Console.WriteLine(dt.ToString("M"));//1月1日 
Console.WriteLine(dt.ToString("d"));//2018-01-01   
Console.WriteLine(dt.ToString("D"));//2018年1月1日 
Console.WriteLine(dt.ToString("t"));//12:05 
Console.WriteLine(dt.ToString("T"));//12:05:05 
Console.WriteLine(dt.ToString("%d"));//1   一月中某天
Console.WriteLine(dt.ToString("%h"));//12   一天中某小时 
Console.WriteLine(dt.ToString("%H"));//12   一天中某小时
Console.WriteLine(dt.ToString("dd"));//01   一月中某天 
Console.WriteLine(dt.ToString("ddd"));//周一   一周中某一天,在AbbreviatedDayNames中定义。  
Console.WriteLine(dt.ToString("dddd"));//星期一   一星期中某一天,在DayNames中定义 
Console.WriteLine(dt.ToString("MM"));//01   一年中某月
Console.WriteLine(dt.ToString("MMM"));//一月   月份的缩写名称,在AbbreviatedMonthNames中定义。 
Console.WriteLine(dt.ToString("MMMM"));//一月   月份的完整名称,在MonthNames中定义。 
Console.WriteLine(dt.ToString("yy"));//18 
Console.WriteLine(dt.ToString("yyyy"));//2018 
Console.WriteLine(dt.ToString("gg"));//公元  
Console.WriteLine(dt.ToString("hh"));//12    12小时制
Console.WriteLine(dt.ToString("HH"));//12    24小时制
Console.WriteLine(dt.ToString("tt"));//下午    上午,下午
Console.WriteLine(dt.ToString("mm"));//05

3.Number

//C 货币
Console.WriteLine(2.5.ToString("C"));//¥2.50
Console.WriteLine(2.5.ToString("C5"));//¥2.50000
//D 十进制数
Console.WriteLine(25.ToString("D5"));//00025,不够前补0
//E 科学型
Console.WriteLine(25000.ToString("E"));//2.500000E+004
//F 固定点
Console.WriteLine(25.ToString("F2"));//25.00
//G 常规
Console.WriteLine(2.5.ToString("G"));//2.5
//N 数字
Console.WriteLine(2500000.ToString("N"));//2,500,000.00
//X 十六进制
Console.WriteLine(256.ToString("X"));//100
//#.00 保留小数位数
Console.WriteLine(256.ToString("#.00"));//256.00
//#,#.00 千位分隔符
Console.WriteLine(25656.ToString("#,#.00"));//256.00
//固定位数(正数部分以及小数部分都是固定位数),不足补零,0是补位,有数字则占位,没则补零
Console.WriteLine(3.5.ToString("0000.00"));//0003.50
Console.WriteLine(30000.5.ToString("0,000.00"));//30,003.50
Console.WriteLine(30000000.5.ToString("0,000.00"));//30,000,000.50
原文地址:https://www.cnblogs.com/lcawen/p/8376563.html