You can use AakashM's solution. If you want something slightly more readable, you can create your own provider:
internal class RatioFormatProvider : IFormatProvider, ICustomFormatter
{
public static readonly RatioFormatProvider Instance = new RatioFormatProvider();
private RatioFormatProvider()
{
}
#region IFormatProvider Members
public object GetFormat(Type formatType)
{
if(formatType == typeof(ICustomFormatter))
{
return this;
}
return null;
}
#endregion
#region ICustomFormatter Members
public string Format(string format, object arg, IFormatProvider formatProvider)
{
string result = arg.ToString();
switch(format.ToUpperInvariant())
{
case "RATIO":
return (result == "0") ? result : "1:" + result;
default:
return result;
}
}
#endregion
}
With this provider, you can create very readable format strings:
int ratio1 = 0;
int ratio2 = 200;
string result = String.Format(RatioFormatProvider.Instance, "The first value is: {0:ratio} and the second is {1:ratio}", ratio1, ratio2);
If you control the class being formatted (rather than a primitive one like Int32), you can make this look nicer. See this article for more details.