简体   繁体   中英

I have a Data Table in which i have a column with decimal values

I have a Data Table with a column of decimal values. I should bind it to a drop down list where the drop down list should display the values of that decimal values in percentage without "%" symbol.I tried the following code in C#.

DropDownList1.DataTextFormatString = "{0:0%}";

0.981 is displayed as 98% in DropDown list , how should I write it to avoid % symbol such that only 98 will get displayed.

The "%" custom specifier multiples your value with 100 and uses PercentSymbol of your CurrentCulture . That means your CurrentCulture has % as a PercentSymbol and this custom speficier has to use it.

As a solution, you can Clone your CurrentCulture and set PercentSymbol to empty string and you can use that culture for your Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture properties.

var clone = (CultureInfo)CultureInfo.InvariantCulture.Clone();
clone.NumberFormat.PercentSymbol = "";
Thread.CurrentThread.CurrentCulture = clone;
Thread.CurrentThread.CurrentUICulture = clone;

now when you do that DropDownList1.DataTextFormatString = "{0:0%}"; , you will get 98 not 98%

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM