Is there any function for nearly equal

gvgbabu

Member
Joined
Sep 22, 2013
Messages
11
Programming Experience
Beginner
Hi
Is there any function for nearly equal in vb.net. For example I have 0.03, 0.043, 0.05 etc I am using =sign in if statement. These all r nearly equal to zero, then it should execute if function. If there is no nearly equal please guide me how to do it.
Thanks
Gvg
 
You would simply write your own method, as you do for any other function. It might be specific or general or both, depending on the requirements, e.g.
Private Function IsNearlyZero(value As Double) As Boolean
    Return IsWithinRangeOf(value, 0.0, 0.05)
End Function

Private Function IsWithinRangeOf(value As Double, target As Double, range As Double) As Boolean
    Return value >= target - range AndAlso value <= target + range
End Function
Maths is maths. Don't think that maths works differently because you're doing it in code.
 
You need to round out the fractional numbers to the nearest integer. You could use the Math.Round() method.

Dim mynum, almost As Double
almost = Math.Round(mynum, 0, MidpointRounding.AwayFromZero)
 
You need to round out the fractional numbers to the nearest integer. You could use the Math.Round() method.

Dim mynum, almost As Double
almost = Math.Round(mynum, 0, MidpointRounding.AwayFromZero)

By my reading of the question, that's not going to give the OP what they want. That will tell you that any number N in the range -0.5 < N < 0.5 is almost equal to zero but I don't think that the OP wants such a wide range.
 
hi jmcilhinney

thanks for your reply. my problem will be not only 0.00 s but also (1.00, 1.043, 1.05.... it should be 1),
(37.0, 37.199, 37.4....it should be 37.4) etc.
so your first post suits for my problem more or less.

thank u very much.
 
Back
Top