Using C # String.Format "{0: p0}" with no leading space before percent sign

Using expression

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

How can I return this value to 10% (without a space between 10 and%)?

Culture: en-GB

+51
string c #
Jun 03 2018-11-11T00:
source share
8 answers

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

+61
Jun 03 2018-11-11T00:
source share

Use the NumberFormatInfo.PercentPositivePattern property:

 NumberFormatInfo numberInfo = new NumberFormatInfo(); numberInfo.PercentPositivePattern = 1; Console.WriteLine(String.Format("{0}", 0.10.ToString("P0",numberInfo))); 
+23
Jun 03 2018-11-11T00:
source share

If you're fine without using Format() , you can do 0.10F.ToString("0%"); .

+6
Jun 03 2018-11-11T00:
source share

Just reinforcing @Jay Riggs answer, and because I don't have enough reputation to just comment, I would go with:

 String.Format(numberInfo, "{0:p0}", 0.10); 

I think you are covering situations where you need to format more than one value:

 String.Format(numberInfo, "{0:p0} {1:p0}", 0.10, 0.20); 
+4
03 Jun. '11 at 20:01
source share
 String.Format("{0:p0}",0.10).Replace(" ",""); 
+2
Apr 15 '16 at 20:50
source share

To add to the other answers, here is the relevant documentation:

+1
Jun 26 '15 at 1:08
source share

Try this instead:

0.1.ToString ("0%")

0
Jun 03 2018-11-11T00:
source share

Change the culture information.

For some crops, it displays% 10,% 10, 10%, 10%,. 1, .10, 0.1, 0.10 ...

I will check which CultureInfo gives you "10%"

0
Jun 03 '11 at 15:30
source share



All Articles