c# – 掩码将格式化为1,10,100,到字符串“001”,“010”,“100”

前端之家收集整理的这篇文章主要介绍了c# – 掩码将格式化为1,10,100,到字符串“001”,“010”,“100”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何将掩码应用于以下列方式格式化输出文本的字符串(最多2个前导零):
int a = 1,b = 10,c = 100;
string aF = LeadingZeroFormat(a),bF = LeadingZeroFormat(b),cF = LeadingZeroFormat(c);
Console.Writeline("{0},{1},{2}",aF,bF,cF); // "001,010,100"

什么是最优雅的解决方案?

提前致谢.

解决方法

您可以使用Int32.ToString(“000”)以此方式格式化整数.详情请参阅 Custom Numeric Format StringsInt32.ToString
string one = a.ToString("000"); // 001
string two = b.ToString("000"); // 010
原文链接:https://www.f2er.com/csharp/93267.html

猜你在找的C#相关文章