Using C# String.Format "{0:p0}" without the leading space before percentage sign
C#StringC# Problem Overview
Using the expression
String.Format("{0:p0}",0.10) gives 10 %
How do I get this to return 10%
(without the space between 10 and %)?
Culture: en-GB
C# Solutions
Solution 1 - C#
String.Format("{0:0%}", 0.10)
Solution 2 - C#
Use the NumberFormatInfo.PercentPositivePattern Property:
NumberFormatInfo numberInfo = new NumberFormatInfo();
numberInfo.PercentPositivePattern = 1;
Console.WriteLine(String.Format("{0}", 0.10.ToString("P0",numberInfo)));
Solution 3 - C#
If you're OK with not using Format()
you could do 0.10F.ToString("0%");
.
Solution 4 - C#
Only enhancing @Jay Riggs response, and because i don´t have enough reputation just to comment, i´d go with:
String.Format(numberInfo, "{0:p0}", 0.10);
I think this way you cover situations where you have to format more than one value:
String.Format(numberInfo, "{0:p0} {1:p0}", 0.10, 0.20);
Solution 5 - C#
Change the culture info.
For some cultures it displays %10 , % 10 , 10 % , 10% , .1 , .10 , 0.1 , 0.10 ...
I will check which CultureInfo gives you "10%"
Solution 6 - C#
To add to the other answers, here's the relevant documentation:
Solution 7 - C#
With a decimal point as well.
String.Format("{0:0.0%}", 0.6493072393590115)
Answer = 64.9%
Solution 8 - C#
String.Format("{0:p0}",0.10).Replace(" ","");
Solution 9 - C#
Try this instead:
0.1.ToString("0%")