Results 1 to 9 of 9

Thread: How do I round a number DOWN?

  1. #1

    Thread Starter
    New Member Cuffbertt's Avatar
    Join Date
    May 2006
    Location
    Yeovil, England
    Posts
    2

    How do I round a number DOWN?

    does anyone know how to round a number down? (like 17.9 to 17 not 18 etc) I'm in a college lesson and I cant seem to find out how to do it, its probably something really simple that i haven't thought of!

  2. #2
    Super Moderator
    Join Date
    Dec 2003
    Posts
    4,787

    Re: How do I round a number DOWN?

    Quote Originally Posted by Cuffbertt
    does anyone know how to round a number down? (like 17.9 to 17 not 18 etc) I'm in a college lesson and I cant seem to find out how to do it, its probably something really simple that i haven't thought of!
    If its allways going to be rounded down then is this true...

    17.9 - 17
    18.2 - 18
    16 .8 - 16

    etc etc

    then the number will be just remove the decimal place?

    So...

    VB Code:
    1. dim temp() as string
    2. dim inumber as string
    3.  
    4. Temp = split(iNumber,".")
    5.  
    6. Msgbox temp(0)

    Try that, I'm not sure if there is a specific function out there for this.

  3. #3
    Fanatic Member noielen's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Phil.
    Posts
    680

    Re: How do I round a number DOWN?

    VB Code:
    1. Dim x As String
    2. Dim inum As Long
    3.  
    4. x = "17.9"
    5.  
    6. inum = CLng(Split(Trim(x), ".")(0))
    7. iround = Round(CLng(x))
    8.  
    9. MsgBox inum & " " & iround
    10.  
    11. If inum < iround Then MsgBox inum
    hehe, jst trying...
    noister
    <advertising link removed by moderator>

  4. #4

    Thread Starter
    New Member Cuffbertt's Avatar
    Join Date
    May 2006
    Location
    Yeovil, England
    Posts
    2

    Re: How do I round a number DOWN?

    Thanks!
    I smell the smelly smell of something that smells smelly!

  5. #5
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: How do I round a number DOWN?

    Please don't use string conversions for a simple task like this. Users can set a decimal seperator in the control panel, so you can't assume the period is the decimal seperator.

    VB has two functions for rounding down: Int and Fix

    For positive numbers, both return the same results, and round the number down.
    eg
    Fix(12.9) = 12
    Int(12.9) = 12
    For negative numbers, Int rounds down, and Fix rounds up
    eg
    Fix(-12.9) = -12
    Int(-12.9) = -13
    Frans

  6. #6
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: How do I round a number DOWN?

    To proof my point, I tested pino's code like this:
    VB Code:
    1. Dim temp() As String
    2. Dim inumber As String
    3. Dim dblNum As Double
    4.  
    5. dblNum = 12.9
    6. inumber = CStr(dblNum)
    7. temp = Split(inumber, ".")
    8.  
    9. MsgBox temp(0)

    The messagebox shows 12,9
    I have the comma as decimal seperator (a standard Dutch setting)
    Frans

  7. #7
    Super Moderator
    Join Date
    Dec 2003
    Posts
    4,787

    Re: How do I round a number DOWN?

    assuming you convert the number to string before hand the code would work fine. Also you just use the type of decimal place your program is outputing...

    As for your test, just change the . to ,

    Its not that complex

  8. #8
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: How do I round a number DOWN?

    I did convert the number to a string, using the CStr function.
    I prefer my code to work with all settings, so using your code, probably some api's are needed to find out what character is set as the decimal seperator in the system.

    Also your code is much slower, because it needs string conversions.
    Frans

  9. #9
    Super Moderator
    Join Date
    Dec 2003
    Posts
    4,787

    Re: How do I round a number DOWN?

    Quote Originally Posted by Frans C
    I did convert the number to a string, using the CStr function.
    I prefer my code to work with all settings, so using your code, probably some api's are needed to find out what character is set as the decimal seperator in the system.

    Also your code is much slower, because it needs string conversions.
    I understand your points but in this situation is the speed diferance (minimal differance) going to have a big effect of the program?

    I doubt it..

    :-/

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