Friday, June 03, 2011

Using C# String.Format “{0:p0}” without the leading space before percentage sign

Simplest expresssion is:  

String.Format("{0:0%}", 0.10)

A more elegant solution is:  

Use the NumberFormatInfo.PercentPositivePattern Property:
NumberFormatInfo numberInfo = new NumberFormatInfo();
numberInfo.PercentPositivePattern = 1;
Console.WriteLine(String.Format("{0}", 0.10.ToString("P0",numberInfo)));