ibrahimyilmaz
ibrahimyilmaz

Reputation: 18919

How to show percentage properly for every culture

As you know, percentage is written left or right side of number in accordance with language.

For example:

In Turkish : %80 | In English : 80%

with localization doing it is really easy.

It is possible to do it with string format and the demonstration can change when culture is changed?

Any idea or help will be appreciated.

Upvotes: 4

Views: 2569

Answers (1)

Andras Zoltan
Andras Zoltan

Reputation: 42353

You say without localization, do you mean globalization (as Leppie says in comments below)? If so - I'm sorry but this answer basically therefore doesn't answer your question, but comes with a strong advisory not to start reinventing lots of wheels.

From Standard numeric format strings:

decimal percentage = 80.5M;
Console.WriteLine(percentage.ToString("P"));

Formats the number as percentage for the current culture.

If it's a particular culture you're after, rather than the current culture, you have two choices - change the current culture (via the CultureInfo.CurrentCulture and CultureInfo.CurrentUICulture members) or you can simply pass the culture as an argument to ToString():

decimal percentage = 80.5M;
//English (UK)
Console.WriteLine(percentage.ToString("P", CultureInfo.CreateSpecificCulture("en-GB")));
//Turkish (Turkey)
Console.WriteLine(percentage.ToString("P", CultureInfo.CreateSpecificCulture("tr-TR")));

Upvotes: 6

Related Questions