我认为您不会找到一个可以为您提供这些结果的格式字符串,但这里有一个IFormatProvider
应该可以工作的自定义:
public class CustomFormatter : IFormatProvider, ICustomFormatter
{
public string Format(string format, object arg, IFormatProvider formatProvider)
{
if(IsNumber(arg))
{
double number = Convert.ToDouble(arg);
if(number < 1)
return string.Format("{0:0.000}", arg);
else if(number < 10)
return string.Format("{0:0.00}", arg);
return string.Format("{0:0}", arg);
}
else return string.Format(format,arg); // default formatting for other types
}
public object GetFormat(Type formatType)
{
return (formatType == typeof(ICustomFormatter)) ? this : null;
}
public static bool IsNumber(object value)
{
return value is sbyte
|| value is byte
|| value is short
|| value is ushort
|| value is int
|| value is uint
|| value is long
|| value is ulong
|| value is float
|| value is double
|| value is decimal;
}
}
void Main()
{
foreach(object val in (new object[] {0, 0.05, 1, 1.0, 1.5, 9.9, 10, 10m,0XFF}))
Console.WriteLine(val + " : "+string.Format(new CustomFormatter(),"{0}",val));
}
输出:
0 : 0.000
0.05 : 0.050
1 : 1.00
1.0 : 1.00
1.5 : 1.50
9.9 : 9.90
10 : 10
10m : 10
255 : 255