Share via

Single DataType evaluation

Peter Volz 1,295 Reputation points
2023-08-11T20:17:39.7933333+00:00

Hello

During a process by a 3rd party SDK, I'm getting the progress completion in percent, but not an integer, but single data type!

To check if it reached 100, I use:

If Convert.ToInt32(e.TotalPercent) = 100 Then ...

The problem is this condition will be false for 99.4026 but once it passes 99.5 like 99.54923 it will be true and I will get many true conditions until it reaches 100.

How to safely evaluate a single data type equals exact 100?

Sorry for the newbie question :)

Developer technologies | VB
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


2 answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 34,241 Reputation points Microsoft External Staff
    2023-09-01T09:14:34.2166667+00:00

    Hi @Peter Volz ,

    You can use a threshold or a tolerance level for comparison. Since it appears that the progress is provided as a floating-point number, you can check if the value is very close to 100 within an acceptable tolerance level.

    Dim progress As Double = e.TotalPercent
    Dim tolerance As Double = 0.01 ' Adjust this tolerance level as needed
    
    If Math.Abs(progress - 100.0) < tolerance Then
    
    End If
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

  2. Bruce (SqlWork.com) 84,086 Reputation points
    2023-08-11T20:26:22.7966667+00:00

    Floating point is not precise. Try

    If Convert.ToInt32(e.TotalPercent * 100.0) = 10000 Then ...

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.