点击下载示例代码

String.Format 一重载方法的签名如下

 public static string Format(
IFormatProvider provider,
string format,
params Object[] args
)

可以通过自定义 IFormatProvider 接口来控制 String.Format 执行过程中的特定行为。

过程如下

1, 自定义类实现 IFormatProvider 接口(object GetFormat 方法)

 public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
else
return null;
}

2, 实现 ICustomFormatter 借口(string Format 方法)

摘自 MSDN。功能为控制 Int64 type 的string输出。

 public string Format(string fmt, object arg, IFormatProvider formatProvider)
{
// Provide default formatting if arg is not an Int64.
if (arg.GetType() != typeof(Int64))
try
{
return HandleOtherFormats(fmt, arg);
}
catch (FormatException e)
{
throw new FormatException(String.Format("The format of '{0}' is invalid.", fmt), e);
} // Provide default formatting for unsupported format strings.
string ufmt = fmt.ToUpper(CultureInfo.InvariantCulture);
if (!(ufmt == "H" || ufmt == "I"))
try
{
return HandleOtherFormats(fmt, arg);
}
catch (FormatException e)
{
throw new FormatException(String.Format("The format of '{0}' is invalid.", fmt), e);
} // Convert argument to a string.
string result = arg.ToString(); // If account number is less than 12 characters, pad with leading zeroes.
if (result.Length < ACCT_LENGTH)
result = result.PadLeft(ACCT_LENGTH, '');
// If account number is more than 12 characters, truncate to 12 characters.
if (result.Length > ACCT_LENGTH)
result = result.Substring(, ACCT_LENGTH); if (ufmt == "I") // Integer-only format.
return result;
// Add hyphens for H format specifier.
else // Hyphenated format.
return result.Substring(, ) + "-" + result.Substring(, ) + "-" + result.Substring();
}

使用实现好的 Format 类

 long acctNumber;
double balance;
DaysOfWeek wday;
string output; acctNumber = ;
balance = 16.34;
wday = DaysOfWeek.Monday; output = String.Format((new AcctNumberFormat(),
"On {2}, the balance of account {0:H} was {1:C2}.",
acctNumber, balance, wday);
Console.WriteLine(output);

执行结果如下

参考资料

Culture invariant Decimal.TryParse()  http://stackoverflow.com/questions/23131414/culture-invariant-decimal-tryparse

What does IFormatProvider do?  http://stackoverflow.com/questions/506676/what-does-iformatprovider-do

IFormatProvider Interface  https://msdn.microsoft.com/en-us/library/system.iformatprovider.aspx

最新文章

  1. war项目在tomcat上面部署
  2. linux小技巧
  3. word20161207
  4. 访问 IIS 元数据库失败 的解决方法
  5. percent-encode 百分号编码
  6. weblogic启动报错之未修改hosts产生错误
  7. 简单使用JSON,通过JSON 字符串来创建对象(二)
  8. SVG文本
  9. 机器学习算法--Perceptron(感知机)算法
  10. Spinner控件详解
  11. PGM:概率论基础知识
  12. 为什么移动构造要使用noexcept
  13. 列式数据库~clickhouse日常管理
  14. javascript五种基本类型
  15. java集合类整理
  16. C++ template —— 类型区分(十一)
  17. vue--非父子组件之间的传值
  18. 何时使用[self release]
  19. Vue 备
  20. MySQL的一些常用sql函数(持续更新。。)

热门文章

  1. python通过xlsxwriter模块将文字写入xlsx文件
  2. Mysql数据库报错1264
  3. 小白该如何学习Linux操作系统
  4. 大数据学习之Hadoop快速入门
  5. 最近最少使用算法(LRU)——页面置换
  6. DOM操作和jQuery实现选项移动操作
  7. LIS(单调队列优化 C++ 版)(施工ing)
  8. 一个servlet如何处理多个请求
  9. 20155218 2006-2007-2 《Java程序设计》第一周学习总结
  10. 20155316 实验一《Java开发环境的熟悉》实验报告