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

C#String

C# 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%")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJulius AView Question on Stackoverflow
Solution 1 - C#Chris WalshView Answer on Stackoverflow
Solution 2 - C#Jay RiggsView Answer on Stackoverflow
Solution 3 - C#YuckView Answer on Stackoverflow
Solution 4 - C#VladimirView Answer on Stackoverflow
Solution 5 - C#Peter Tok'Ra GeorgievView Answer on Stackoverflow
Solution 6 - C#cprcrackView Answer on Stackoverflow
Solution 7 - C#smoore4View Answer on Stackoverflow
Solution 8 - C#WatsonView Answer on Stackoverflow
Solution 9 - C#Barry KayeView Answer on Stackoverflow