ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Returning just the integer portion of a number (https://www.excelbanter.com/excel-programming/390887-returning-just-integer-portion-number.html)

George Burdell

Returning just the integer portion of a number
 
How do I return the integer portion of a number? for example 508.5 or
508.49 or 508.51 all should return 508

I have tried this::

Sub test()
Dim anyNum As Double
Dim I As Integer
anyNum = 508.51
I = anyNum \ 1
MsgBox I
End Sub


However, VB is first rounding anyNum in I = anyNum \ 1 and then doing the
\ 1 operation. Thus 508.51 returns 509 instead of the desired value of 508.

Thanks in advance!




PCLIVE

Returning just the integer portion of a number
 
Try this:

Sub test()
Dim anyNum As Double
Dim I As Integer
anyNum = 508.51
I = Int(anyNum)
MsgBox I
End Sub

HTH,
Paul

"George Burdell" wrote in message
. ..
How do I return the integer portion of a number? for example 508.5 or
508.49 or 508.51 all should return 508

I have tried this::

Sub test()
Dim anyNum As Double
Dim I As Integer
anyNum = 508.51
I = anyNum \ 1
MsgBox I
End Sub


However, VB is first rounding anyNum in I = anyNum \ 1 and then doing
the \ 1 operation. Thus 508.51 returns 509 instead of the desired value of
508.

Thanks in advance!






George Burdell

Returning just the integer portion of a number
 
Your example works, but when I put it into my macro, which performs math on
the anyNum value, it doesn't always work :((

for example:

Sub test()
Dim anyNum As Double
Dim I As Integer
anyNum = 48.4
anyNum = anyNum / 0.1
I = Int(anyNum)
MsgBox I
End Sub

returns 483 instead of 484. If I do a watch on "anyNum / 0.1" and anyNum
both are 484 right before I is assigned. But I is assigned the value 483
by using Int(). I think what is happening is that anyNum /0.1 is actually
483.99999999999, thus the value of 483 as the answer. The only solution
I've come up with is to join anyNum with a text string ("x"), find the
decimal in the text string, extract the number between the "x" and the
decimal, and assign it to I.

Sub test2()
Dim anyNum As Double
Dim I As Integer
Dim N As Integer
Dim nDiv As Double
Dim S As String
anyNum = 48.4
nDiv = 0.1 'but can vary (0.01, 0.001, etc)
anyNum = anyNum / nDiv
S = "x" & anyNum & "." ' adding "." in case no "." in anynum
N = InStr(S, ".")
I = Mid(S, 2, N - 2)
MsgBox I
End Sub

That's what I call a super kludge! Anyone have any better ideas?


"PCLIVE" wrote in message
...
Try this:

Sub test()
Dim anyNum As Double
Dim I As Integer
anyNum = 508.51
I = Int(anyNum)
MsgBox I
End Sub

HTH,
Paul

"George Burdell" wrote in message
. ..
How do I return the integer portion of a number? for example 508.5 or
508.49 or 508.51 all should return 508

I have tried this::

Sub test()
Dim anyNum As Double
Dim I As Integer
anyNum = 508.51
I = anyNum \ 1
MsgBox I
End Sub


However, VB is first rounding anyNum in I = anyNum \ 1 and then doing
the \ 1 operation. Thus 508.51 returns 509 instead of the desired value
of 508.

Thanks in advance!








[email protected]

Returning just the integer portion of a number
 
On 7 Jun, 14:39, "George Burdell" wrote:
How do I return the integer portion of a number? for example 508.5 or
508.49 or 508.51 all should return 508


Sub test()
Dim anyNum As Double
Dim I As Integer
anyNum = 508.51
I = int(anyNum)
MsgBox I
End Sub

Simpler, no?

--
juux


PCLIVE

Returning just the integer portion of a number
 
How about this:

Sub test2()
Dim anyNum As Double
Dim I As Integer
anyNum = CLng(48.4 / 0.1)
I = anyNum
MsgBox I
End Sub




"George Burdell" wrote in message
. ..
Your example works, but when I put it into my macro, which performs math
on the anyNum value, it doesn't always work :((

for example:

Sub test()
Dim anyNum As Double
Dim I As Integer
anyNum = 48.4
anyNum = anyNum / 0.1
I = Int(anyNum)
MsgBox I
End Sub

returns 483 instead of 484. If I do a watch on "anyNum / 0.1" and anyNum
both are 484 right before I is assigned. But I is assigned the value 483
by using Int(). I think what is happening is that anyNum /0.1 is actually
483.99999999999, thus the value of 483 as the answer. The only solution
I've come up with is to join anyNum with a text string ("x"), find the
decimal in the text string, extract the number between the "x" and the
decimal, and assign it to I.

Sub test2()
Dim anyNum As Double
Dim I As Integer
Dim N As Integer
Dim nDiv As Double
Dim S As String
anyNum = 48.4
nDiv = 0.1 'but can vary (0.01, 0.001, etc)
anyNum = anyNum / nDiv
S = "x" & anyNum & "." ' adding "." in case no "." in anynum
N = InStr(S, ".")
I = Mid(S, 2, N - 2)
MsgBox I
End Sub

That's what I call a super kludge! Anyone have any better ideas?


"PCLIVE" wrote in message
...
Try this:

Sub test()
Dim anyNum As Double
Dim I As Integer
anyNum = 508.51
I = Int(anyNum)
MsgBox I
End Sub

HTH,
Paul

"George Burdell" wrote in message
. ..
How do I return the integer portion of a number? for example 508.5 or
508.49 or 508.51 all should return 508

I have tried this::

Sub test()
Dim anyNum As Double
Dim I As Integer
anyNum = 508.51
I = anyNum \ 1
MsgBox I
End Sub


However, VB is first rounding anyNum in I = anyNum \ 1 and then doing
the \ 1 operation. Thus 508.51 returns 509 instead of the desired value
of 508.

Thanks in advance!










Ron Rosenfeld

Returning just the integer portion of a number
 
On Thu, 7 Jun 2007 10:42:16 -0400, "George Burdell"
wrote:

I think what is happening is that anyNum /0.1 is actually
483.99999999999, thus the value of 483 as the answer. The only solution
I've come up with is to join anyNum with a text string ("x"), find the
decimal in the text string, extract the number between the "x" and the
decimal, and assign it to I.


You're correct, although the number is a bit less that 483.999999999999999.

One thought:

Sub test()
Dim anyNum
Dim I As Integer
anyNum = 48.4
anyNum = CDec(anyNum / 0.1)
I = Int(anyNum)
Debug.Print I
End Sub
--ron


All times are GMT +1. The time now is 05:53 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com