.NET Framework提供了方法,能够将任何数值、枚举以及日期和时间等基数据类型表示为字符串
格式化由格式说明符字符的字符串控制,该字符串指示如何表示基类型值
例如,格式说明符指示:是否应该用科学记数法来表示格式化的数字
例如:格式字符"C",说明货币格式
同时.NET Framework还使用区域性设置,以便用适合于特定区域性的形式表示基类型。
我们可以提供自定义的区域性设置,或者使用与当前线程关联的默认区域性设置。
例如,格式化货币类型的时候,区域性设置指定用于货币符号
要是我们想拥有自己定义的格式化,.NET Framework也允许我们定义自己格式化方案和自定义区域性设置。
例如:我想格式字符"MyFormat",来说明我自定义的格式,即在字符前加三个***
关于数字格式字符串,可以参考类
System.Globalization.NumberFormatInfo
关于日期与时间格式字符串,可以参考类
System.Globalization.DateTimeFormatInfo
先看看IFormattable接口的原型
public interface IFormattable
{
// Methods
string ToString(string format, IFormatProvider formatProvider);
}
参数说明:
format
指定要使用的格式的 String
当为空引用时,表示使用为 IFormattable 实现的类型定义的默认格式
formatProvider
用于格式化该值的 IFormatProvider
当为空引用时,从操作系统的当前区域设置中获取格式信息的
一些基本的值类型实现了该接口,例如:
Int32 ,UInt32 , DateTime ,Guid ,类Enum
再看看IFormatProvider接口的原型
public interface IFormatProvider
{
// Methods
object GetFormat(Type formatType);
}
参数说明:
formatType
一个对象,它指定要获取的格式对象的类型
NumberFormatInfo、DateTimeFormatInfo和CultureInfo实现IFormatProvider接口
NumberFormatInfo提供数字格式信息,如用于小数分隔符和千位分隔符的字符,以及货币值中货币符号的拼写和位置
DateTimeFormatInfo提供与日期相关和与时间相关的格式信息,如日期模式中月、日和年的位置
CultureInfo包含特定区域性中的默认格式信息,其中包括数字格式信息以及与日期相关和与时间相关的格式信息
再看看ICustomFormatter接口的原型
public interface ICustomFormatter
{
// Methods
string Format(string format, object arg, IFormatProvider formatProvider);
}
参数说明:
format
包含格式规范的格式字符串
arg
要格式化的对象
formatProvider
一个 IFormatProvider 对象,它提供有关当前实例的格式信息
在arg为空引用时,引发异常
如果 format 为空引用 ,将使用默认格式规范
如果 formatProvider 为空引用 ,则忽略该参数
好了,说了这么多
我们来动手来实现格式字符"MyFormat",在字符前加三个***的需求
定义一个类
using System;

namespace MyFormat
{
public class MyClass : System.IFormattable

...{
Double d;

public MyClass(Double d)

...{
this.d=d;
}

public string ToString(string format, IFormatProvider formatProvider)

...{
return (format=="MyFormat")?"***"+d.ToString(formatProvider):d.ToString(format,formatProvider);
}
}
} 再到一控制台中
System.Globalization.CultureInfo culture=null;

MyClass myClass=new MyClass(5);
//当IFormatProvider为空时,调用的是当前线程关联的文化信息
Console.WriteLine("显示中国货币格式:{0}",myClass.ToString("C",null));

culture=System.Globalization.CultureInfo.CurrentCulture;
Console.WriteLine("显示当前系统默认货币格式:{0}",myClass.ToString("C",culture));

culture=new System.Globalization.CultureInfo("zh-HK");
Console.WriteLine("显示香港特别行政区货币格式:{0}",myClass.ToString("C",culture));

Console.WriteLine("显示我自己定义的货币格式:{0}",myClass.ToString("MyFormat",null));
Console.ReadLine();