Option Compare Database   'Use database order for string comparisons

'Function CS_Round (n,d) 'Revised 3/23/94 by Bliss Sloan, CDM Associates
'*****************************************************************
'Accepts: a variant value n and a numerical d number of decimal places
'Purpose: converts multiplace decimal numbers
'Returns: the number n "formatted" to d decimal places.
'
'   Bliss's notes:
'   I modified Christopher Garcia's routine, which
'   only handled very small d (up to 4?), to handle
'   large d, up to 125--this works on my system, at least.
'   Values of d larger than the decimal
'   accuracy in Access will return the rounded number with extra 0's
'   on the end, out d places.
'   I also renamed the function so you won't get conflicts with
'       other Round functions you may already have.  Feel free to
'       rename it back to just Round from CS_Round.
'   Again, if you have any suggestions, please feel free to modify this
'       and let me (and the others below!) know...
'   --Bliss Sloan, CDM Associates [73770,1501] 3/23/94
'
'   Christopher Garcia's notes:
'         I used the
'         original Format () designed by Marcus O. M. Grabe
'         CIS 100120,1405, but I found that dur to the fact that
'         Access inherently does not round up when it comes face
'         to face with a value of 5 (I'd like to meet the genius that
'         came up with that one) I had to include the if statements
'         to add the .001, .0001, etc. to round up if it met
'         certain criteria, as you can see below. I am by no means an
'         experienced programmer, so if you have any suggestions, please feel
'         free to modify this, all I ask is that you let me know, so I can
'         make the changes myself.
'
'               Christopher V. Garcia, CIS 72623,3437
'*****************************************************************
Function CS_Round (n As Variant, d As Variant) As Variant
    'round the number n to up to 125 d decimal places
    'note: decimal places are further limited by Access implementation
    'by Bliss Sloan, CDM Associates

    If IsNull(n) Or IsNull(d) Then
	'arbitrarily return 0 if either parameter is null
	Sum = 0
    Else
	'initialize sum to n to start--assume already rounded
	Sum = n

	'make sure number of decimal places makes sense (int >= 0)
	'   by arbitrarily removing nonintegral and negative parts
	d = IIf((d < 0), 0, Int(d))

	If d > 0 Then
	    'make sure requested number of decimal places was not
	    '   so large as to crash.  If it was, set it back to
	    '   125 (which works on bliss's machine)
	    If (d > 125) Then
		d = 125
	    End If

	    'set format strings to round arbitrary numbers of dec. pts.
	    '   by setting one to "#,###." followed by d+1 0's to check
	    '   for .5 rounding, another to d 0's for final answer
	    'format string which will have d+1 0's:
	    Dim strFormatDPlus1 As String
	    'format string which will have d 0's
	    Dim strFormatD As String
	    'loop variable for building the format strings
	    Dim intDecimalCounter As Integer
	    strFormatD = "#,###."
	    For intDecimalCounter = 1 To d
		strFormatD = strFormatD & "0"
	    Next intDecimalCounter
	    strFormatDPlus1 = strFormatD & "0"

	    'do the actual rounding
	    If Right(Format(n, strFormatDPlus1), 1) = 5 Then
		Sum = Format(n + .001, strFormatD)
	    Else
		Sum = Format(n, strFormatD)
	    End If

       Else 'd = 0 here, just return the number sans decimal point
	    Sum = Format(n, "#,###")
       End If
    End If
    CS_Round = Sum 'set return value of function to the temporary sum
End Function
                                                                                                                            