Link to home
Start Free TrialLog in
Avatar of Brian
BrianFlag for United States of America

asked on

Format Decimal to Percentage value

Hello,

I have data in my DB in a decimal format Precision value = 8 Scale = 4 (8, 4) and I need to display the acutall percentage value rather than the decimal value and I had to insert that data as a decimal format in my DB so it retained the deciaml value. Is there any way that I can convert this to a percentage value in my GridView column?

Example of data in my DB below:

88.25 in DB its 88.2500
86.042 in DB its 86.0420
20 in DB its 20.0000
3.45 in DB its 3.4500
101.575 in DB its 101.5750
Avatar of corneliu_news
corneliu_news

select cast(86.0420 as decimal(12,2))
You can have the percentage with two decimal places

use DataFormatString="{0:P2}" in the gridview field
Avatar of Brian

ASKER

Hi masterpass,

When i include that format string my values look like the following below which is incorrect.

8,990.00 %
9,392.50 %
10,000.00 %
Can you tell me what is the value coming and how you want it rendered ?

for eg : Do you want it as 89.90 ?
Avatar of Brian

ASKER

The following values below are in my DB as a decimal value (8, 4) and I will put what they should be formatted as.

IN DB:                  NEED TO DISPLAY:
88.2500              88.25% or 88.2%
1.5000                1.5% or 1.50%
0.4000                0.4%
1.0000                 1%
Can you try to use

{0:D2}%
Avatar of Brian

ASKER

I receive an error: "Format specifier was invalid."
yeah , It is a decimal formatter ... if you need a percentage formatter, the earlier format is the one ... by default it will be multiplied with 100 and then add % to it ....
Avatar of Brian

ASKER

So you are saying to use this format string? "{0:P2}"

I did apply this format string but instead of displaying just 1% it displayed 100.00% which is wrong.
Avatar of Brian

ASKER

well it doesn't look good then because i even tried to apply {0:P4} when {0:P2} did not work as needed and i got the following result below for {0:P4}

9,130.0000 %
2 stands for 2 decimal places and 4 stands for 4 decimal places .... Whwn you apply this format string It will MULTIPLY the value by 100 and append a % symbol to it !!!. So I think you can do some formatting in SQL in the select statement
Avatar of Brian

ASKER

what do you recommend for the sql format? My decimal fieds precision 8 and the scale is 4
Frankly I am a No No to SQL ... So no ideas about it !!!

Just see if this helps DataFormatString="{0:###.## %}"
Avatar of Brian

ASKER

Hi masterpass,

Ok, one of the decimal values in my DB is 88.2500 and when I implement the DataFormatString above it converted that to 8825% and I need it to display 88.25%.
See the idea about the format string for percentage is that you don't have to do any calculations.
What ever value you give to the grid, it will multiply with 100 and append a % symbol to it .
So modify the select statement you are using to retrieve the number/100

i.e If you want 86.85 % in grid then you need to give the value as .8685 to the DB. so that it will multiply with 100 and show. I don't think there is another way out to show the format string
Avatar of Brian

ASKER

Ok, so I'm not going to use a format string because I believe that I resolved the issue with the way that I was setting my precision and scale on the decimal field within SQL.

The one thing though that you may be able to help me with is how can I add just the "%" symbol next to my BoundField Column within the GridView?
so how is the data coming to the grid now ?

86.85 or .8685 ?
Avatar of Brian

ASKER

86.85
then I think you will have to add the % in the SQL itself. The reason is as soon as the % symbol is seen in dataformat string, it will get multiplied with 100. There is no other way to format data other dataformatstring. You can add % from code behind if you use datatable as the datasource ...
Avatar of Brian

ASKER

Do you always have to use {0:P} or can you just use [P} to just display the % sign?
you cannot use {P}, it will give you input was not in correct format error .. so you should use {0:P} to get the % symbol ...
Avatar of Brian

ASKER

yeah, but if I include [0:P] then it displays it as an integer following by the % sign which is unexceptable.
yeah .. because you will have to specify the number of decimal places

by {0:Pn} ---> n represents number of decimal places

If you want 2 decimal places, then {0:P2}
Avatar of Brian

ASKER

awwww,

when using {0:P2} is displays 61.5 as 6,150.00%
that is what I said, two options

1. send 61.5 as 0.65 from the DB so that it will render as 61.5%

2. send 61.5 as 61.5 from the DB itself

I would recommend the first one though !!!
Avatar of Brian

ASKER

how do i do that?
ASKER CERTIFIED SOLUTION
Avatar of masterpass
masterpass
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brian

ASKER

Thank you VERY much masterpass, I went with the val/100 after all because I needed to display the % for the user.

Thanks again for sticking through it with me to help out a noob...