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 :)

C#
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.
11,409 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,823 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 74,531 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 ...

    0 comments No comments

  2. Jiachen Li-MSFT 34,206 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.

    0 comments No comments

Your answer

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