生成此文件所需的 ToString 格式是什么
> 5 to 00000500
> 10 to 00001000
> 15 to 00001500
> 20 to 00002000
谢谢。
生成此文件所需的 ToString 格式是什么
> 5 to 00000500
> 10 to 00001000
> 15 to 00001500
> 20 to 00002000
谢谢。
如果您只想使用ToString
格式字符串执行此操作,而不是也涉及乘法,那么您可以使用以下内容:
string yourString = yourNumber.ToString("000000'00'");
尝试做
(yourNum * 100).ToString("0000000");
(number * 100).ToString("D8");
您可以使用自定义格式提供程序来做到这一点。前提是输入是一个字符串。不过,它很容易更改为任何数据类型。只需将其更改if (arg.GetType() == typeof(String))
为您想要格式化的任何内容。
用法:
var five = "5";
var ten = "10";
var fiveteen = "15";
var twenty = "20";
var formatprovider = new CustomStringFormatProvider();
var res1 = string.Format(formatprovider, "{0}", five);
res1 = string.Format(formatprovider, "{0}", ten);
res1 = string.Format(formatprovider, "{0}", fiveteen);
res1 = string.Format(formatprovider, "{0}", twenty);
这是格式提供程序
public sealed class CustomStringFormatProvider : IFormatProvider, ICustomFormatter
{
public Object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter)) return this;
return Thread.CurrentThread.CurrentCulture.GetFormat(formatType);
}
public String Format(String format, Object arg, IFormatProvider formatProvider)
{
String s;
IFormattable formattable = arg as IFormattable;
if (formattable == null) s = arg.ToString();
else s = formattable.ToString(format, formatProvider);
if (arg.GetType() == typeof(String))
return String.Join("",Enumerable.Repeat("0", 4).ToArray()) + s + String.Join("",Enumerable.Repeat("0", 4 - s.Length).ToArray());
return s;
}
}