• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 642
  • Last Modified:

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
0
asp_net2
Asked:
asp_net2
  • 14
  • 14
1 Solution
 
corneliu_newsCommented:
select cast(86.0420 as decimal(12,2))
0
 
masterpassCommented:
You can have the percentage with two decimal places

use DataFormatString="{0:P2}" in the gridview field
0
 
asp_net2Author Commented:
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 %
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
masterpassCommented:
Can you tell me what is the value coming and how you want it rendered ?

for eg : Do you want it as 89.90 ?
0
 
asp_net2Author Commented:
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%
0
 
masterpassCommented:
Can you try to use

{0:D2}%
0
 
asp_net2Author Commented:
I receive an error: "Format specifier was invalid."
0
 
masterpassCommented:
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 ....
0
 
asp_net2Author Commented:
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.
0
 
asp_net2Author Commented:
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 %
0
 
masterpassCommented:
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
0
 
asp_net2Author Commented:
what do you recommend for the sql format? My decimal fieds precision 8 and the scale is 4
0
 
masterpassCommented:
Frankly I am a No No to SQL ... So no ideas about it !!!

Just see if this helps DataFormatString="{0:###.## %}"
0
 
asp_net2Author Commented:
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%.
0
 
masterpassCommented:
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
0
 
asp_net2Author Commented:
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?
0
 
masterpassCommented:
so how is the data coming to the grid now ?

86.85 or .8685 ?
0
 
asp_net2Author Commented:
86.85
0
 
masterpassCommented:
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 ...
0
 
asp_net2Author Commented:
Do you always have to use {0:P} or can you just use [P} to just display the % sign?
0
 
masterpassCommented:
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 ...
0
 
asp_net2Author Commented:
yeah, but if I include [0:P] then it displays it as an integer following by the % sign which is unexceptable.
0
 
masterpassCommented:
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}
0
 
asp_net2Author Commented:
awwww,

when using {0:P2} is displays 61.5 as 6,150.00%
0
 
masterpassCommented:
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 !!!
0
 
asp_net2Author Commented:
how do i do that?
0
 
masterpassCommented:
select val/100 as valueinpercent from tablename where conditions  
0
 
asp_net2Author Commented:
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...
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 14
  • 14
Tackle projects and never again get stuck behind a technical roadblock.
Join Now