TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
 Specialized Dev Zones Research Center eBook Library .NET Java C++ Web Dev Architecture Database Security Open Source Enterprise Mobile Special Reports 10-Minute Solutions DevXtra Blogs Slideshow
 Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Language: Visual Basic
Expertise: Beginner
May 12, 1999

### WEBINAR:On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning

# Compare Floating Point Values Using the Round Function

When you have to compare the results of floating point expressions, you can't rely on the "=" operator due to the finite precision of Single or Double variables. To see this concept demonstrated, use this code:
``````
Dim i As Integer, d As Double
For i = 1 To 10
d = d + CDbl(0.1)
Next
MsgBox (d = 1)	' displays "False"
``````
To more easily compare two floating numbers, use the new VB6 Round function, which rounds a number to the desired number of decimal digits. For example, you can rewrite the previous test like this:
``````
' the difference is less than 1E-12
MsgBox (Round(d, 12) = 1)  ' displays "True"
``````
You can also use this method to check whether A and B variables contain values that match up to their 12th decimal digit:
``````
If Round(A - B, 12) = 0 Then Print "Equal"
``````
Francesco Balena

 Submit a Tip Browse "XML" Tips Browse All Tips
Comment and Contribute

(Maximum characters: 1200). You have 1200 characters left.

Thanks for your registration, follow us on our social networks to keep up-to-date