Weird percentages

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When displaying a calculated percentage the first time, I got a number that
was 1/100th of what it should be and that went on to about 10 decimal points.
So I used my ingenuity & multiplied the figure by 100. But I can't seem to
limit the number of digits to 1 past the decimal point...
 
Sue:

There are tthree ways to do this, the first two using Access' built in
functionality and a third using a VBA routine.

1.) For any control you are working with on a form or report, you can
specify the format of the control to be a percentage and set the decimal
places to display. In a query field, you can also specify the format of the
field and its decimal places as well by right clicking on the field and
choosing its properties.

2.) You can also use the Format function, but beware, Format returns a
String, so you can't manipulate the number or add it with others. e.g.
=Format ([YourField],"Percent")

3.) A third way to limit the decimal places is to use the VBA function
below, which is a rounding function (with a lot of flexibility, unlike
Access' built in functions)

HTH
--
Steve Arbaugh
ACG Soft
http://ourworld.compuserve.com/homepages/attac-cg

'----------begin code-----------
Public Function Round(ByVal Number As Variant, NumDigits As Long, _
Optional UseBankersRounding As Boolean = False) As Double
'Posted by Ken Getz in Microsoft.public.access.modulesdaovba
'1998/11/24
'The algorithm can round a number ending with 5 thousandths
'to a number ending with an even-numbered hundredths (banker's rounding).
'e.g. if Bankers = True then Round(8.565 ,2,true) = 8.56

'--------------------
Dim dblPower As Double
Dim varTemp As Variant
Dim intSgn As Integer

If Not IsNumeric(Number) Then
' Raise an error indicating that
' you've supplied an invalid parameter.
Err.Raise 5
End If
dblPower = 10 ^ NumDigits
' Is this a negative number, or not?
' intSgn will contain -1, 0, or 1.
intSgn = Sgn(Number)
Number = Abs(Number)

' Do the major calculation.
varTemp = CDec(Number) * dblPower + 0.5

' Now round to nearest even, if necessary.
If UseBankersRounding Then
If Int(varTemp) = varTemp Then
' You could also use:
' varTemp = varTemp + (varTemp Mod 2 = 1)
' instead of the next If ...Then statement,
' but I hate counting on TRue == -1 in code.
If varTemp Mod 2 = 1 Then
varTemp = varTemp - 1
End If
End If
End If
' Finish the calculation.
Round = intSgn * Int(varTemp) / dblPower
End Function
'-----------end code---------------
 
Back
Top