how to turn off percentage sign in .net/wpf?

2019-08-17 18:53发布

问题:

I would like my WPF application to display all percentages without % sign. For example 20% would be displayed as "20" but i still want to use the standard formatting for percentages (so i get the benefit of string formatter to multiply it by 100 for me)

in other words, how do i get string.Format("0.00%", 0.2) to output "20" but not "20%"?

Is it possible to globally define PercentSymbol as empty string for the entire application?

In particular i am using ContentStringFormat in my WPF application to format the numbers and percentages. Maybe i can do it directly in WPF.

回答1:

Provide your own NumberFormatInfo to String.Format. Example based on the invariant culture:

NumberFormatInfo info = (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone();
info.PercentSymbol = String.Empty;
string s = String.Format(info, "{0:00%}", 0.02);


回答2:

In theory the way you would do this is to create your own custom CultureInfo for your application. Then you would create your own NumberFormatInfo. In this you can define the PercentSymbol.