Someone posted a thread looking for a way to round a number to the nearest quarter (.25).
This thread must have been deleted before I had a chance to respond. Here is how you do it:
Harry W posted a solution as I remember, but it did not work correctly:Code:If (mynumber / 0.25) * 100 Mod 100 Then 'checks to see if number really needs rounding 'UpDown = 0.5 rounds up or down to nearest quarter 'UpDown = 0 rounds down to nearest quarter 'UpDown = 1 rounds up to nearest quarter UpDown = 0.5 mynumber = Format((Int(mynumber * 4 + UpDown)) / 4, "$0.00") Else mynumber = Format(mynumber, "$0.00") 'number did not need rounding End If
You can try this little example out:Code:'This code will Round down: dblValue = (Int(dblValue * 4 + 1)) / 4 'This code will round up: dblValue = (Int(dblValue * 4 + 2)) / 4 'This code will round to the nearest 0.25: dblValue = (Int(dblValue * 4 + 1.5)) / 4
[Edited by dsy5 on 10-15-2000 at 03:30 PM]Code:Private Sub Form_Load() Dim mynumber As String Dim arr arr = Array(1.73, 1.25, 0.03, 0.15, 0.37, 0.92, 1.75, 1.5, 0.62, 2.38, 1200.23) For x = 0 To UBound(arr) mynumber = Format(arr(x), "0.00") 'Round up to a quarter If (mynumber / 0.25) * 100 Mod 100 Then 'checks to see if number really needs rounding 'UpDown = 0.5 rounds up or down to nearest quarter 'UpDown = 0 rounds down to nearest quarter 'UpDown = 1 rounds up to nearest quarter UpDown = 0.5 Debug.Print mynumber; " "; Format((Int(mynumber * 4 + UpDown)) / 4, "$0.00") Else Debug.Print mynumber; " "; Format(mynumber, "0.00") 'number did not need rounding End If Next x arr = Array(vbNull) End Sub




Reply With Quote