Solved

# round a number to the next highest number,regardless of value of reciprocal

Posted on 2003-12-11
349 Views
dim x as single

if its x.0
i want it to stay x

if its x.1 - x.9 i want to to change to (x+1)

thanks!
0
Question by:jxharding

LVL 28

Expert Comment

ID: 9920508
Will this work?

Private Sub Command1_Click()
x = 1.9
MsgBox Round(x + 0.4)
End Sub
0

LVL 9

Expert Comment

ID: 9920519
jxharding,
Add .5 to the value as you round it.

Dang123
0

LVL 10

Expert Comment

ID: 9920536

Dim x as Single
IF x - Abs ( x ) > 0 THEN
x = Abs ( x ) + 1
END IF
0

LVL 13

Expert Comment

ID: 9920815

Function FixIt(n As Single)
FixIt = n + IIf(n <> Int(n), (1 - (n - Int(n))), 0)
End Function
0

LVL 6

Expert Comment

ID: 9921465
try this:

msgbox Convert(5.0)
msgbox Convert(5.1)
...
msgbox Convert(5.9)

Private Function Convert(strVal As Single)
Convert = IIf((strVal * 10) \ 10 = strVal, strVal, Int(strVal + 1))
End Function

cheers,
srimanth.
0

LVL 17

Expert Comment

ID: 9921915
So that you get uniform results with negatives here is a simple but comprehensive function.

Hope this helps:~)

Function RoundToNext(psngNumeric As Single) As Single ' or whatevery type you need.

RoundToNext = Sgn(psngNumeric) * Int(Abs(psngNumeric) + 0.9999)

End Function
0

LVL 4

Expert Comment

ID: 9923119
Dim x As Single
Dim y() As Single
Dim z As Single

Private Sub Command1_Click()
x = Text1.Text
End Sub
If InStr(1, x, ".") Then
y() = Split(x, ".")
z = x - y(0)
x = x + (0.1 - z)
End If
MsgBox x
Exit Sub

that might work, I'm not at home right now so i can't test it...
0

LVL 4

Accepted Solution

learning_t0_pr0gram earned 250 total points
ID: 9923598
sorry.. got home and fixed it, should be:

Dim x As Single
Dim y() As String
Dim z As Single

Private Sub Command1_Click()
x = Text1.Text
If InStr(1, x, ".") Then
y() = Split(x, ".")
z = x - y(0)
x = x + (1 - z)
End If
MsgBox x
End Sub
0

## Featured Post

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…