|
-
Jul 15th, 2000, 02:42 PM
#1
Thread Starter
New Member
Can anyone help me use the format command to round down and up number ?
Thanks
-
Jul 15th, 2000, 03:21 PM
#2
What do you need the format function for?
Code:
Public Function Round(Num As String) As Integer
Dim NumDec As String
On Error GoTo extrnd
If IsNumeric(Num) = False Or InStr(Num, ".") = False Then
GoTo extrnd 'Make sure the parameter num is numeric, and a Decimal before trying to round it
Else
NumDec = Mid(Num, InStr(Num, "."), Len(Num)) 'We find all the numbers To the right of the decimal.
NumDec = Right(NumDec, Len(NumDec) - 1) 'We Get rid of the "." before the number
End If
If Left(NumDec, 1) >= 5 Then 'Find out if the FIRST number in the Decimal is greater than or equal To 5
Num = Int(Num) + 1 'If so, drop the decimal, Then add one
Else
Num = Int(Num) 'If not, just drop the decimal.
End If
Round = Num
extrnd:
Exit Function
End Function
'9.4 = 9
'9.5 = 10
Hope that helps.
-
Jul 15th, 2000, 03:46 PM
#3
Thread Starter
New Member
Firstly Thanks !
I would like to use format to format a number with X numbers after the decimal.
-
Jul 15th, 2000, 03:54 PM
#4
This code will round off a decimal to specified number of places.
Code:
Public Function SigFig(value As Double, SigFigs As Long) _
As Double
'Rounds off the significant figures to SigFigs places
'ie, (3.14159, 4) = 3.142; (12345678.431,3)=12300000 etc
Dim Ss As Variant
Dim i As Long
Dim S As String
Dim OOM As Long 'Order of Magnitude
Dim doneSet As Boolean
Dim TotalS As String
'~25mS/100 on 233MHz NT machine (~170mS/1000)
'Split requires VB6, or use VB5 version available on
'FreeVBCode.com
Ss = Split(value, ".")
If UBound(Ss, 1) = 1 Then
'you've got a real number with valid figures
'after the decimal
If Len(Ss(0)) = 1 And Ss(0) = "0" Then
'you've got no integer portion
OOM = -1
For i = 1 To Len(Ss(1))
S = Mid(Ss(1), i, 1)
If S = "0" And doneSet = False Then
OOM = OOM - 1
End If
If S <> "0" Then doneSet = True
If doneSet = True Then TotalS = TotalS & S
Next i
Else
'integer portion dictates Order Of Magnitude
OOM = Len(Ss(0)) - 1
TotalS = Ss(0) & Ss(1)
End If
Else
'integer
OOM = Len(Ss(0)) - 1
TotalS = Ss(0)
End If
If SigFigs > Len(TotalS) Then SigFig = value: Exit Function
S = ""
For i = 1 To SigFigs + 1
S = S & Mid(TotalS, i, 1)
Next i
S = CStr(CLng(Val(S) / 10))
SigFig = S / 10 ^ (Len(S) - 1)
S = SigFig & "E" & OOM
SigFig = S
End Function
-
Jul 15th, 2000, 04:48 PM
#5
In VB6 you can use the built-in Round function. Round(45.456, 1) returns 45.5 and Round(45.456, 2) returns 45.46, etc.
-
Jul 15th, 2000, 04:52 PM
#6
Thanks for pointing that out MartinLiss. I wouldn't know the because I don't have Vb 6.0.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|