Results 1 to 6 of 6

Thread: Format Command / Rounding Numbers

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    10

    Question

    Can anyone help me use the format command to round down and up number ?

    Thanks

  2. #2
    Guest
    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    10

    Smile

    Firstly Thanks !
    I would like to use format to format a number with X numbers after the decimal.

  4. #4
    Guest
    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

  5. #5

  6. #6
    Guest
    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
  •  



Click Here to Expand Forum to Full Width