View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
eric eric is offline
external usenet poster
 
Posts: 1
Default truncating a number in VB

Ron,
How can I subtract these two numbers and get zero?

-----Original Message-----
On Thu, 26 Aug 2004 12:41:24 -0700, "Eric"

wrote:

How do I truncate numbers in VB Excel(2000)?


I am in Excel's VB and having problems getting the VB to
see the two figures below as the same amount. I have

tried
it as a single and double, but it doesn't work.

number1= 1.06000201020
number2= 1.06000606060

I'd like excel to see it as
number1=number2
not
number1<number2


Well, there are a bunch of ways of doing this in VB.

You can Round the numbers to the desired degree of

precision and test for
equality.

For example:

?round(1.0600020102,4)=round(1.0600060606,4)
True

You could decide how closely you want the numbers to

agree and right a little
routine to compare them:

===================
Sub foo()
Const number1 As Double = 1.0600020102
Const number2 As Double = 1.0600060606

Const Difference As Double = 10 ^ -5

If Abs(number1 - number2) < Difference Then
MsgBox (number1 & " is the same as " & number2)
Else
MsgBox (number1 & " is not the same as " & number2)
End If

End Sub
=========================

And there are other ways, too, depending on what you want

to do with the
result.


--ron
.