I have a variable Amount in my class.
This Amount value is coming from database as decimal something .0.000654345
While displaying in grid I need to show this as % value.
Some languages, such as C#, also support the conversion of Decimal values to Char values. If the result of these conversions cannot be represented in the destination type, an OverflowException exception is thrown. The Decimal type also provides methods that convert Decimal values to and from Single and Double values.
To convert a percentage to a decimal, divide by 100. So 25% is 25/100, or 0.25. To convert a decimal to a percentage, multiply by 100 (just move the decimal point 2 places to the right). For example, 0.065 = 6.5% and 3.75 = 375%.
ToString() is a method in C# that is called on decimal values. It converts a numeric value that is a decimal to a string.
Syntax: public static int ToInt32 (decimal value); Here, the value is the decimal number which is to be converted. Return Value: It returns a 32-bit signed integer equivalent to the specified value.
Use the "P" format, which is hopefully more aware of globalization:
String.Format("{0:P4}", pos); // e.g. 1,000.0000%
You ought to be able to use this format string directly in your Gridview, too, as others have suggested. Since it does the 100x calc for you, though, it should actually work :). Perhaps like this:
<asp:BoundField DataField="Db" DataFormatString="{0:P4}" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With