Julius A
Julius A

Reputation: 39612

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

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

Upvotes: 79

Views: 50974

Answers (9)

smoore4
smoore4

Reputation: 4866

With a decimal point as well.

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

Answer = 64.9%

Upvotes: 4

Watson
Watson

Reputation: 1425

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

Upvotes: 2

cprcrack
cprcrack

Reputation: 19119

To add to the other answers, here's the relevant documentation:

Upvotes: 3

Vladimir
Vladimir

Reputation: 264

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);

Upvotes: 7

Jay Riggs
Jay Riggs

Reputation: 53593

Use the NumberFormatInfo.PercentPositivePattern Property:

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

Upvotes: 37

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%"

Upvotes: 5

Yuck
Yuck

Reputation: 50835

If you're OK with not using Format() you could do 0.10F.ToString("0%");.

Upvotes: 12

Barry Kaye
Barry Kaye

Reputation: 7761

Try this instead:

0.1.ToString("0%")

Upvotes: 0

Chris Walsh
Chris Walsh

Reputation: 1903

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

Upvotes: 93

Related Questions