Mironline
Mironline

Reputation: 2799

Percentage in StringFormat

I have a technical problem with using percentage in StringFormat method.

The result of String.Format("{0:P}", 0.8526) is 85.26%

but I think it should be 0.8526%

Is it correct ? If yes , How can I get 0.8526% ?

Upvotes: 16

Views: 44244

Answers (4)

Pandor Cai
Pandor Cai

Reputation: 216

Console.WriteLine(string.Format("{0}%", 0.8526));

Upvotes: 20

Michel Keijzers
Michel Keijzers

Reputation: 15357

A percentage is considered from 0-100% and the related floating pointer number from 0.0-1.0 as it is a ratio between two numbers.

Upvotes: 2

Jordan
Jordan

Reputation: 2758

String.Format will multiply by 100 when you use % or :p String.Format You should divide by 100 first if you want to get what you are looking for.

Upvotes: 2

Kaf
Kaf

Reputation: 33809

Yes mathematically 0.8526 is equal to 85.26%. If you need to get 0.8526% then try this

String.Format("{0:P}", 0.8526/100)

Upvotes: 17

Related Questions