Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default truncating a number in VB

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default truncating a number in VB

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
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,327
Default truncating a number in VB

Hi

I like using "significant digits" for this, it scales the slack according to
number size. Your needs may be different, but here we go anyway:

Function SigDig(ByVal D As Double, i As Long) As Double
Dim L As Long
L = i - 1 - Int(Log(Abs(D)) / Log(10#))
SigDig = Round(D * 10 ^ L, 0) / (10 ^ L)
End Function

Sub tester()
Dim number1 As Double
Dim number2 As Double
number1 = 1.0600020102
number2 = 1.0600060606
MsgBox SigDig(number1, 2)
MsgBox SigDig(number1, 3)
MsgBox SigDig(number1, 3) = SigDig(number2, 3)
End Sub

HTH. Best wishes Harald
"Eric" skrev i melding
...
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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default truncating a number in VB

This could be an awkward way to do it but it will do the
job and truncate the numbers to 2 decimals:

Dim number1 As Double
Dim number2 As Double


number1 = Left(Range("A1").Value, InStr(1, Range
("A1").Value, ".", vbTextCompare) - 1) & Mid(Range
("A1").Value, InStr(1, Range("A1").Value, ".",
vbTextCompare), 3)

number2 = Left(Range("A2").Value, InStr(1, Range
("A2").Value, ".", vbTextCompare) - 1) & Mid(Range
("A2").Value, InStr(1, Range("A2").Value, ".",
vbTextCompare), 3)


this example assumes that your number1 is in cell A1 and
number2 is in cell A2. if you want to truncate to more
than 2 decimals, change the number at the end of each
line, for example from ), 3) to ), 4) to have 3 decimals
  #5   Report Post  
Posted to microsoft.public.excel.programming
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
.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default truncating a number in VB

On Thu, 26 Aug 2004 13:26:24 -0700, "eric" wrote:

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


Uh, by rounding them to a level of precision where they are identical.

eg:

res = Round(Number1,4) - Round(Number2,4)

res will equal zero given the two numbers in your example.


--ron
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Truncating a VIN lputnam Excel Discussion (Misc queries) 3 November 11th 09 05:28 PM
text truncating Overcome by info Excel Discussion (Misc queries) 2 October 1st 07 04:16 PM
truncating series Michael Charts and Charting in Excel 0 May 31st 06 11:41 AM
Truncating numbers Susana C via OfficeKB.com Excel Worksheet Functions 1 May 8th 06 04:42 PM
truncating and rounding Chang Excel Programming 1 November 20th 03 05:00 PM


All times are GMT +1. The time now is 06:48 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"