Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Do you know what the overload/order part returns? 0.
In the debugger, it even listed overload = 1 and order = 5 and overload / order = 0. Explain what the problem is.




C#
public void Progress(int overload)
        {
            progressBar.Value = ((overload / order) * 100);
        }
Posted

If you try to do something like below,

int intAsFloat = 0.2; then compiler will complain. To solve the issue we need to change the data type of intAsFloat variable from int to float(for example).

In your example code overload and (I assume) order also as int and when you divide 1/5 then it produce 0.2 and try to store that result with value 0 and then multiply by 100 and eventually you get 0. you could try below,

C#
public static void Progress(float overload, float order)
{
    var result = ((overload / order) * 100);
}


hope it helps :)
 
Share this answer
 
Comments
Mohammad A Rahman 22-Jul-11 20:24pm    
:)
thatraja 23-Jul-11 0:02am    
/*I think I'll just marry you.*/
:D :D What's going on here?
Mohammad A Rahman 23-Jul-11 0:32am    
:)
Kim Togo 23-Jul-11 2:03am    
B-)
shefeekcm 23-Jul-11 2:55am    
????????????????????????
Isn't it obvious? Your result is perfectly explained by integer division.

By the way, you would have much more luck if you multiply first:

C#
int result = 100 * overload / order;


Better yet, work with floating point number all the time but round it to integer only when you show the result on screen:

C#
double result = 100d * overload / order;
int uiResult = (int)System.Math.Round(result);


—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900