Results 1 to 18 of 18

Thread: Split Integers *RESOLVED*

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2002
    Posts
    19

    Split Integers *RESOLVED*

    I have a number like 3785.12345
    I want to return only the 3785 part, the part before the deciaml. i dont want the other part. any idea people
    Last edited by NavyDog; Aug 20th, 2002 at 12:45 AM.

  2. #2
    Lively Member
    Join Date
    Jul 2002
    Posts
    68
    very easy
    VB Code:
    1. dim intvar as long
    2. intvar = 3785.12345
    3. int(intVar)

    Or...

    VB Code:
    1. Dim splitarray as string, strText as string
    2. strText = "3785.12345"
    3. split(strText,".")
    4. msgbox strText(0)
    -Maddox

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2002
    Posts
    19
    what are you smokin, that don't work, it don't even make since

  4. #4
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228
    VB Code:
    1. Public Function RoundDown(Number)
    2.  RoundDown = Left(Number, InStr(1, Number, "."))
    3. End Function
    It will round down any number. Usage:
    VB Code:
    1. MsgBox RoundDown(123.4553) 'Will return 123.
    T
    Last edited by macai; Aug 19th, 2002 at 01:09 AM.
    Luke

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2002
    Posts
    19
    thanks that worked but it left the deciaml so i made it

    Replace(Left(Text1, InStr(1, Text1, ".")), ".", "")

    by the way how do i add vb code. it has php code but not vb code

  6. #6
    Junior Member
    Join Date
    Aug 2002
    Posts
    24
    You could also use the Fix function.

    Code:
    Dim sngX As Single
    Dim intX As Integer
    
    sngX = 1234.5678
    intX = Fix(sngX)
    MsgBox "sngX = " & sngX & "      intX = " & intX
    Last edited by moondance; Aug 19th, 2002 at 07:23 AM.

  7. #7
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    You better use functions like Int, CInt, Round or Fix, and don't use functions like Split, or Instr Left combinations because not all countries use the point as decimal seperator (We use a comma as decimal separator).

    eg Macai's RoundDown function returns an empty string when called like this:
    MsgBox RoundDown(4 / 3)

  8. #8
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228
    Originally posted by Frans C
    You better use functions like Int, CInt, Round or Fix, and don't use functions like Split, or Instr Left combinations because not all countries use the point as decimal seperator (We use a comma as decimal separator).

    eg Macai's RoundDown function returns an empty string when called like this:
    MsgBox RoundDown(4 / 3)
    It only does that if you have 1 digit in the number's first part. Try
    changing the START expression to zero.
    Luke

  9. #9
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    It won't work at all with dutch locale settings. Your own example returns an empty string as well:

    VB Code:
    1. Private Sub Command1_Click()
    2.     MsgBox RoundDown(123.4553) 'Will return an empty string
    3. End Sub

    This is because there is no "." character in the number.
    With the Dutch locale settings 123.4553 reads as 123,4553

    If you want to get the integer part of a number, use the Int function instead. That is what it was made for.

    MsgBox Int(123.4553) ' will return 123

  10. #10
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228
    OK heres my rewrite:
    VB Code:
    1. Public Function RoundDown(Number, Optional Delim As String = ".")
    2.  RoundDown = Left(Number, InStr(1, Number, Delim))
    3. End Function
    Problem solved.
    Luke

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

    That works (almost, I had to remove the comma as well).
    Because the decimal seperator is a system setting, you can't hardcode it. So the function call now looks a bit akward because I need to know the delimiter first.

    VB Code:
    1. Public Function RoundDown(Number, Optional Delim As String = ".")
    2.     RoundDown = Left(Number, InStr(1, Number, Delim) - 1)
    3. End Function
    4.  
    5. Private Sub Command1_Click()
    6.     MsgBox RoundDown(123.4553, Mid(Format(1.1, "0.0"), 2, 1)) 'Will return 123
    7. End Sub


    I hope you don't mind if I keep using the Int function

  12. #12
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228
    I don't care, i just prefer to hardcode as much as possible.
    Luke

  13. #13
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    As mentioned, if you want to use a built in function then
    Fix is for you.

    From MSDN:
    Returns the integer portion of a number.

    Syntax

    Int(number)

    Fix(number)

    The required number argument is a Double or any valid numeric expression. If number contains Null, Null is returned.

    Remarks

    Both Int and Fix remove the fractional part of number and return the resulting integer value.

    The difference between Int and Fix is that if number is negative, Int returns the first negative integer less than or equal to number, whereas Fix returns the first negative integer greater than or equal to number. For example, Int converts -8.4 to -9, and Fix converts -8.4 to -8.

    Fix(number) is equivalent to:

    Sgn(number) * Int(Abs(number))

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Aug 2002
    Posts
    19
    i am reciving that number from a half-life server. it will always have a decimal no matter what. i dont need to know about the decimal crap. and that int i dont think will work because the 234.24354 is a Single. it might but i dont know.

  15. #15
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    one more:

    msgbox 234.24354 \ 1
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  16. #16
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by NavyDog
    i am reciving that number from a half-life server. it will always have a decimal no matter what. i dont need to know about the decimal crap. and that int i dont think will work because the 234.24354 is a Single. it might but i dont know.
    It does work... both of them. So, make up your mind: your best choices are Fix or Int.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  17. #17
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    I have a number like 3785.12345
    I want to return only the 3785 part
    BuggyP, didn't we go around the bouy on this on a few days ago too

    Bruce.

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Aug 2002
    Posts
    19
    im using Int()

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