|
-
Aug 19th, 2002, 12:40 AM
#1
Thread Starter
Junior Member
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.
-
Aug 19th, 2002, 12:48 AM
#2
Lively Member
very easy
VB Code:
dim intvar as long
intvar = 3785.12345
int(intVar)
Or...
VB Code:
Dim splitarray as string, strText as string
strText = "3785.12345"
split(strText,".")
msgbox strText(0)
-
Aug 19th, 2002, 12:53 AM
#3
Thread Starter
Junior Member
what are you smokin, that don't work, it don't even make since
-
Aug 19th, 2002, 01:05 AM
#4
Frenzied Member
VB Code:
Public Function RoundDown(Number)
RoundDown = Left(Number, InStr(1, Number, "."))
End Function
It will round down any number. Usage:
VB Code:
MsgBox RoundDown(123.4553) 'Will return 123.
T
Last edited by macai; Aug 19th, 2002 at 01:09 AM.
Luke
-
Aug 19th, 2002, 01:14 AM
#5
Thread Starter
Junior Member
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
-
Aug 19th, 2002, 07:09 AM
#6
Junior Member
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.
-
Aug 19th, 2002, 07:59 AM
#7
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)
-
Aug 19th, 2002, 09:00 AM
#8
Frenzied Member
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.
-
Aug 19th, 2002, 09:10 AM
#9
It won't work at all with dutch locale settings. Your own example returns an empty string as well:
VB Code:
Private Sub Command1_Click()
MsgBox RoundDown(123.4553) 'Will return an empty string
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
-
Aug 19th, 2002, 09:24 AM
#10
Frenzied Member
OK heres my rewrite:
VB Code:
Public Function RoundDown(Number, Optional Delim As String = ".")
RoundDown = Left(Number, InStr(1, Number, Delim))
End Function
Problem solved.
-
Aug 19th, 2002, 09:32 AM
#11
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:
Public Function RoundDown(Number, Optional Delim As String = ".")
RoundDown = Left(Number, InStr(1, Number, Delim) - 1)
End Function
Private Sub Command1_Click()
MsgBox RoundDown(123.4553, Mid(Format(1.1, "0.0"), 2, 1)) 'Will return 123
End Sub
I hope you don't mind if I keep using the Int function
-
Aug 19th, 2002, 05:08 PM
#12
Frenzied Member
I don't care, i just prefer to hardcode as much as possible.
-
Aug 19th, 2002, 06:19 PM
#13
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))
-
Aug 19th, 2002, 07:44 PM
#14
Thread Starter
Junior Member
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.
-
Aug 19th, 2002, 07:46 PM
#15
The picture isn't missing
one more:
msgbox 234.24354 \ 1
Remember, if someone's post was not helpful, you can always rate their post negatively  .
-
Aug 19th, 2002, 07:51 PM
#16
Need-a-life Member
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.
-
Aug 19th, 2002, 07:52 PM
#17
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.
-
Aug 19th, 2002, 08:00 PM
#18
Thread Starter
Junior Member
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
|