Results 1 to 12 of 12

Thread: Rounding

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2000
    Location
    Isle of Man
    Posts
    276
    How can I make sure a number is always rounded up, regardless of what follows the decimal point

    Int() and Fix() seem to caouse it to be rounded down.

  2. #2

  3. #3
    Hyperactive Member
    Join Date
    Feb 2000
    Posts
    284
    Well I guess depending on how many decimal points you can have you could always add 0.999.... to the number and then round it, but don't give me a hard time on this one if it is complete rubbish, I am on my lunch break here.

  4. #4
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    Results using the ROUND() function recommended by kovan.

    ? ROUND(1234.1962, 3) && Displays 1234.1960
    ? ROUND(1234.1962, 2) && Displays 1234.2000
    ? ROUND(1234.1962, 0) && Displays 1234.0000
    ? ROUND(1234.1962, -1) && Displays 1230.0000
    ? ROUND(1234.1962, -2) && Displays 1200.0000
    ? ROUND(1234.1962, -3) && Displays 1000.0000


    Syntax

    ROUND(expression[,numdecimalplaces])

    If you omit the numdecimalplaces, an integer is returned.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2000
    Location
    Isle of Man
    Posts
    276
    well, i've kinda sorted it like this, but it's a dodgy as hell kinda way of doing it: -

    Code:
    Public Function RoundUp(Num As Long) As Integer
    Dim NumStr, TempStr1, TempStr2 As String
    Dim X As Integer
    
        NumStr = CStr(Num)
        TempStr1 = ""
        TempStr2 = ""
        
        For X = 1 To Len(NumStr)
            TempStr1 = Mid(NumStr, X, 1)
            If TempStr1 = "." Then
                Exit For
            Else
                TempStr2 = TempStr2 + TempStr1
            End If
        Next X
        
        RoundUp = (CInt(TempStr2) + 1)
            
    End Function
    there has to be a mathematical way of doing this - but i struggle to add up, never mind do hard stuff

  6. #6
    Hyperactive Member Paul Warren's Avatar
    Join Date
    Jun 2000
    Location
    UK
    Posts
    282
    For info. VB does some strange rounding when it rounds a decimal to an integer on your behalf. When the decimal is in the middle ( .5 ) then for odd numbers it rounds up and evens number round down ! 1.5 and 2.5 are both rounded to 2.

    Try this code if you don't beleive me :

    Code:
    Private Sub Form_Load()
    
    Dim sTemp1 As String
    Dim sTemp2 As String
    Dim iTemp1 As Integer
    Dim iTemp2 As Integer
    
    ' Remember this lesson when changing values from string to integer
    sTemp1 = "1.5"
    sTemp2 = "2.5"
    iTemp1 = Val(sTemp1)
    iTemp2 = Val(sTemp2)
    
    MsgBox iTemp1 & "  " & iTemp2, vbOKOnly, "VB Rounding"
    
    End Sub
    That's Mr Mullet to you, you mulletless wonder.

  7. #7
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    Well, then ROUND() is not going to be very reliable.

    In the immediate window you get.

    ? ROUND(1.5, 0)
    2
    ? ROUND(2.5, 0)
    2

    Paul, thanks for passing that on.

  8. #8
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    Option Explicit
    'rounding a number 
    
    Function RoundNumber(lNumber, Optional iDecimalPlaces As Integer = 1)
        RoundNumber = Int(lNumber * (10 ^ iDecimalPlaces) + 0.5) / (10 ^ iDecimalPlaces)
    End Function
    
    '3 represents the number of places you want after the decimal
    Private Sub Command1_Click()
        MsgBox RoundNumber(34567.8987, 3)
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2000
    Location
    Isle of Man
    Posts
    276
    that last one seems to round up OR down depending upon the value of the leftmost digit being removed, as normal meths would have it, but for my situation I am converting days to months for insurance cover - ie 35 days needs to be rounded up into a 2 month policy...

  10. #10
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523
    Do you consider one month as 31 days? If yes then try this:
    Code:
        Dim inDays As Integer: inDays = Text1.Text
        Dim inMonths As Integer
        
        If inDays Mod 31 <> 0 Then
            inMonths = inDays \ 31 + 1
        Else
            inMonths = inDays \ 31
        End If
        
        MsgBox inMonths '//If inDays = 35 then inMonths returns 2
    HTH

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2000
    Location
    Isle of Man
    Posts
    276

    OK, similar thing, now

    how can I force a number to have 2 decimal places??

    ie 3 would appear as 3.00

    tried Round(3,2), but just reduces the number of decimal places to 2, rather than increasing them

  12. #12
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    Look up the Format command and see the different ways it allows you to format numbers, dates, strings.

    For example: format(3,"#.00")

    returns 3.00

    format(3.9,"#.00")

    returns 3.90

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