vhpcomp
asked on
ASP solution to format number as percentage?
Using vbscript, is the FormatPecent function available? When I tried to format a rst field I received an error.
I return a rst field from SQL that is already multiplied by 100 in the back end, so without applying any client side formatting, the numbers display like this:
8.36
4.7
4.26
I want to display:
8.36%
4.70%
4.26%
What is the most efficient way to accomplish this using vbscript ASP?
Thanks!
I return a rst field from SQL that is already multiplied by 100 in the back end, so without applying any client side formatting, the numbers display like this:
8.36
4.7
4.26
I want to display:
8.36%
4.70%
4.26%
What is the most efficient way to accomplish this using vbscript ASP?
Thanks!
You can use formatpercent(), but remember that doing so will multiply your number by 100.
So FormatPercent(8.36) will give you 836%
So do it this way:
response.write(FormatPerce nt(objRS(" YourField" )*100))
FtB
So FormatPercent(8.36) will give you 836%
So do it this way:
response.write(FormatPerce
FtB
a better one would be
<%
x = 8.555
if Len(x) < 4 then
x = x&"0"
response.write x
else
y = Round(x,2)
response.write y & "%"
end if
%>
<%
x = 8.555
if Len(x) < 4 then
x = x&"0"
response.write x
else
y = Round(x,2)
response.write y & "%"
end if
%>
my original post didnt take into account those numbers that didnt have at least 2 decimal places
ASKER
fritz the blank,
my complete line of code is
Response.Write("<td><font face='Arial' size='2'>" & rst("percent_sent") &"</font></td>")
If I try to use
Response.Write("<td><font face='Arial' size='2'>" & FormatPercent( rst("percent_sent"))&"</fo nt></td>")
I receive a type mismatch.
As far as the result, I guess I could either divide it by 100 as you suggest (you say multiply but I'm pretty sure you meant divide..)
or I can change my back end process so I don't multiply there.
my complete line of code is
Response.Write("<td><font face='Arial' size='2'>" & rst("percent_sent") &"</font></td>")
If I try to use
Response.Write("<td><font face='Arial' size='2'>" & FormatPercent( rst("percent_sent"))&"</fo
I receive a type mismatch.
As far as the result, I guess I could either divide it by 100 as you suggest (you say multiply but I'm pretty sure you meant divide..)
or I can change my back end process so I don't multiply there.
Please give this a shot:
Response.Write("<td><font face='Arial' size='2'>" & FormatPercent(rst("percent _sent")*.0 1) &"</font></td>")
Response.Write("<td><font face='Arial' size='2'>" & FormatPercent(rst("percent
You are right, I did mean divide, but *.01 should do it.
FtB
FtB
ASKER
fritz_the_blank,
Still receive the type mismatch error, but I believe it has something to do with the underlying rst field rather than being a problem with the FormatPercent function,
because I get a type mismatch if I do ANY kind of mathmatical operation, for example
dim myVal
myVal = rst("percent_sent")
myVal = myVal/100----------------- ---------- ---------- --->I get a type mismatch here also
Here's the SQL for the field, if that helps....
CAST(CAST(call_ct AS decimal)
/ CAST(pnrcount AS decimal) * 100 AS decimal(9, 2))
Still receive the type mismatch error, but I believe it has something to do with the underlying rst field rather than being a problem with the FormatPercent function,
because I get a type mismatch if I do ANY kind of mathmatical operation, for example
dim myVal
myVal = rst("percent_sent")
myVal = myVal/100-----------------
Here's the SQL for the field, if that helps....
CAST(CAST(call_ct AS decimal)
/ CAST(pnrcount AS decimal) * 100 AS decimal(9, 2))
Do you get an error if you do this?
Response.write(CDbl(rst("p ercent_sen t")))
FtB
Response.write(CDbl(rst("p
FtB
ASKER
no error,it works ok
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks ftb, that works!
Vince
Vince
Glad to have helped,
FtB
FtB
x = 8.55555
y = Round(x,2)
response.write y & "%"
%>