Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change decimal to % using .net

Tags:

asp.net

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.

like image 785
kumar Avatar asked May 14 '10 20:05

kumar


People also ask

Can I use decimal in C#?

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.

How do you change a decimal to a?

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%.

Can we convert decimal to string in C#?

ToString() is a method in C# that is called on decimal values. It converts a numeric value that is a decimal to a string.

How do I return a decimal in C#?

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.


1 Answers

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}" />
like image 80
Michael Haren Avatar answered Sep 18 '22 16:09

Michael Haren