Link to home
Create AccountLog in
Avatar of vhpcomp
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!
Avatar of flow79
flow79
Flag of United States of America image

<%
x = 8.55555
y = Round(x,2)
response.write y & "%"
%>
Avatar of fritz_the_blank
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(FormatPercent(objRS("YourField")*100))


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
%>
my original post didnt take into account those numbers that didnt have at least 2 decimal places
Avatar of vhpcomp
vhpcomp

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"))&"</font></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.
Please give this a shot:

Response.Write("<td><font face='Arial' size='2'>" & FormatPercent(rst("percent_sent")*.01) &"</font></td>")
You are right, I did mean divide, but *.01 should do it.

FtB
Avatar of vhpcomp

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))

 
Do you get an error if you do this?

Response.write(CDbl(rst("percent_sent")))

FtB
Avatar of vhpcomp

ASKER

no error,it works ok
ASKER CERTIFIED SOLUTION
Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of vhpcomp

ASKER

Thanks ftb, that works!
Vince
Glad to have helped,

FtB