Re: ASP and VBScript

by "Joshua Graham" <jagraham(at)ozemail.com.au>

 Date:  Wed, 31 Mar 1999 08:12:34 +1000
 To:  <hwg-software(at)hwg.org>
  todo: View Thread, Original
Ellen,

Two likely solutions to this one - you've got the first bit of both of them.

1) Multiply by 100, (implicitly) convert to string, take the first 5
characters, append a percent sign

dblDatabaseField = 0.12345678
dblCentime = dblDatabaseField * 100
sPercent = Left(CStr(dblCentime), 5) & "%"

RESULT: 12.34%

2) Multiply by 100, round to two decimal places, (implicitly) convert to
string, append a percent sign

dblDatabaseField = 0.12345678
dblCentime = dblDatabaseField * 100
dblCentimeRound = Round(dblDatabaseField, 2)
sPercent = CStr(dblCentimeRound) & "%"

RESULT: 12.35%


>From this you can see that the two approaches implement the same type of
effect as the VB Fix and Int functions, respectively. The first will strip
of non-significant digits and the second will round (up or down) at the
least signigicant digit.

As with any rounding or fixing, either will introduce fraction errors in
your data and steps should be taken if the result is not a final output.
That is, if you use this number in an algorithm and errors should not be
propogated, use the original database field value through the code and just
use your converted string representation for output.

Most VB functionality is in VBScript so you can refer to any VB
documentation to get you most of the way there. Some COM objects dislike
implicit string conversion so use the CStr function if passing to a
non-VBScript routine. The above code does not need the CStr but is included
for completeness.

Also, you can obviously cut down the conversion code to one line for
brevity. For example, option 2 could read:

dblDatabaseField = 0.12345678
sPercent = Round(dblDatabaseField * 100, 2) & "%"


The MSDN Library CDROM and MSDN Online have full reference material for
JScript and VBScript for use in both ASP (server) and DHTML (client) script.

Hope this helps,
Josh Graham
Graham Information Science Pty Ltd
jagraham(at)ozemail.com.au



-----Original Message-----
From: mcmahoe1(at)nationwide.com <mcmahoe1(at)nationwide.com>
>One of the fields that is returned is stored in the
>database as 0.xxxxxxxxx and I need to display it as XX.XX%. I am just
learning
>ASP and VBScript, so I can't figure out how to reformat the field. I've
included
>a * 100 in the sql to change the 0.xxxxxxxx to xx.xxxxxxxxxx, but don't
know how
>to do the rest of it
>
>Ellen McMahon

HWG: hwg-software mailing list archives, maintained by Webmasters @ IWA