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
Printable View
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
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)
what are you smokin, that don't work, it don't even make since
It will round down any number.:) Usage:VB Code:
Public Function RoundDown(Number) RoundDown = Left(Number, InStr(1, Number, ".")) End FunctionTVB Code:
MsgBox RoundDown(123.4553) 'Will return 123.
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
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
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. TryQuote:
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)
changing the START expression to zero. :)
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
OK heres my rewrite:Problem solved.VB Code:
Public Function RoundDown(Number, Optional Delim As String = ".") RoundDown = Left(Number, InStr(1, Number, Delim)) End Function
:D 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:D
I don't care, i just prefer to hardcode as much as possible. :)
As mentioned, if you want to use a built in function then
Fix is for you.
From MSDN:
Quote:
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))
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.
one more:
msgbox 234.24354 \ 1
It does work... both of them. So, make up your mind: your best choices are Fix or Int.Quote:
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.
BuggyP, didn't we go around the bouy on this on a few days ago too :DQuote:
I have a number like 3785.12345
I want to return only the 3785 part
Bruce.
im using Int()