simple calculation not working for some reason - c#

Alright, I'm trying to calculate the percentage of two values. This should be really simple but for some weird reason it's not working. I'm too tired/dumb to figure it out.
Here's my code, it keeps returning 0, i checked the values while debugging and with FilesCompleted being 295 and TotalFilesCount being 25002 the returnvalue var is just 0, it should be 1 already.
private int CalculatePercentComplete(int FilesCompleted, int TotalFilesCount)
{
int returnvalue = (FilesCompleted / TotalFilesCount) * 100;
if (returnvalue > 100 || returnvalue < 1) return 1;
else return returnvalue;
}

i checked the values while debugging and with FilesCompleted being 295 and TotalFilesCount being 25002 the returnvalue var is just 0, it should be 1 already.
No, because all the arithmetic is being done with integers. So first this expression is evaluated:
(FilesCompleted / TotalFilesCount)
That's 295 / 25002. The result of that integer arithmetic is 0... and when you then multiply it by 100, you've still got 0. The simplest fix is just to do the multiplication first:
int returnvalue = (FilesCompleted * 100) / TotalFilesCount;
Note that that will overflow if FilesCompleted is greater than int.MaxValue / 100. You could fix that by either doing everything in floating point arithmetic:
int returnvalue = (int)((FilesCompleted * 100.0) / TotalFilesCount);
... or by using long integer arithmetic:
int returnvalue = (int)((FilesCompleted * 100L) / TotalFilesCount);
Neither of these are necessary if you don't expect to have an insane number of files, of course. (You're fine up to 42 million files...)
As a side note, your parameter names violate .NET naming conventions. They should be camelCased - totalFilesCount and filesCompleted.

How about
int returnvalue = (int)(((double)FilesCompleted / TotalFilesCount) * 100);
What this is doing
Converting int FilesCompleted to double. For eg if its 295, then this will convert it into 295.0 so that division happens in double.
There is no need to convert TotalFilesCount to double also as divison of double by integer (or integer by double) returns a double.
So the returned double result is 0.011799056075513958 which is multipled by 100
So the retunred result is 1.1799056075513958 which is finally converted to int which returns 1

The FilesCompleted/TotalFilesCount returns 0.01 which in int format is 0, try FilesCompleted*100/TotalFilesCount!

Indeed, simple error, when doing diversions with int, the answer will be an int. Your answer would be between 0 and 1 so rounded down it is 0 (int).
Use:
int returnvalue = (int)((FilesCompleted / (double)TotalFilesCount) * 100);
To make your calculation use a double for the fractional number and then multiply that by 100 and cast that to an int.

Related

changing celsius to fahrenheit and vice versa in c# [duplicate]

This simple calculation is returning zero, I can't figure it out:
decimal share = (18 / 58) * 100;
You are working with integers here. Try using decimals for all the numbers in your calculation.
decimal share = (18m / 58m) * 100m;
18 / 58 is an integer division, which results in 0.
If you want decimal division, you need to use decimal literals:
decimal share = (18m / 58m) * 100m;
Since some people are linking to this from pretty much any thread where the calculation result is a 0, I am adding this as a solution as not all the other answers apply to case scenarios.
The concept of needing to do calculations on various types in order to obtain that type as a result applies, however above only shows 'decimal' and uses it's short form such as 18m as one of the variables to be calculated.
// declare and define initial variables.
int x = 0;
int y = 100;
// set the value of 'x'
x = 44;
// Results in 0 as the whole number 44 over the whole number 100 is a
// fraction less than 1, and thus is 0.
Console.WriteLine( (x / y).ToString() );
// Results in 0 as the whole number 44 over the whole number 100 is a
// fraction less than 1, and thus is 0. The conversion to double happens
// after the calculation has been completed, so technically this results
// in 0.0
Console.WriteLine( ((double)(x / y)).ToString() );
// Results in 0.44 as the variables are cast prior to calculating
// into double which allows for fractions less than 1.
Console.WriteLine( ((double)x / (double)y).ToString() );
Because the numbers are integers and you perform integer division.
18 / 58 is 0 in integer division.
Whenever I encounter such situations, I just upcast the numerator.
double x = 12.0 / 23409;
decimal y = 12m / 24309;
Console.WriteLine($"x = {x} y = {y}");
double res= (firstIntVar * 100f / secondIntVar) / 100f;
when dividing numbers I use double or decimal , else I am getting 0 , with this code even if firstIntVar && secondIntVar are int it will return the expected answer
decimal share = (18 * 100)/58;
Solved: working perfectly with me
int a = 375;
int b = 699;
decimal ab = (decimal)a / b * 100;

Lamda expression not working on String.Count() [duplicate]

This simple calculation is returning zero, I can't figure it out:
decimal share = (18 / 58) * 100;
You are working with integers here. Try using decimals for all the numbers in your calculation.
decimal share = (18m / 58m) * 100m;
18 / 58 is an integer division, which results in 0.
If you want decimal division, you need to use decimal literals:
decimal share = (18m / 58m) * 100m;
Since some people are linking to this from pretty much any thread where the calculation result is a 0, I am adding this as a solution as not all the other answers apply to case scenarios.
The concept of needing to do calculations on various types in order to obtain that type as a result applies, however above only shows 'decimal' and uses it's short form such as 18m as one of the variables to be calculated.
// declare and define initial variables.
int x = 0;
int y = 100;
// set the value of 'x'
x = 44;
// Results in 0 as the whole number 44 over the whole number 100 is a
// fraction less than 1, and thus is 0.
Console.WriteLine( (x / y).ToString() );
// Results in 0 as the whole number 44 over the whole number 100 is a
// fraction less than 1, and thus is 0. The conversion to double happens
// after the calculation has been completed, so technically this results
// in 0.0
Console.WriteLine( ((double)(x / y)).ToString() );
// Results in 0.44 as the variables are cast prior to calculating
// into double which allows for fractions less than 1.
Console.WriteLine( ((double)x / (double)y).ToString() );
Because the numbers are integers and you perform integer division.
18 / 58 is 0 in integer division.
Whenever I encounter such situations, I just upcast the numerator.
double x = 12.0 / 23409;
decimal y = 12m / 24309;
Console.WriteLine($"x = {x} y = {y}");
double res= (firstIntVar * 100f / secondIntVar) / 100f;
when dividing numbers I use double or decimal , else I am getting 0 , with this code even if firstIntVar && secondIntVar are int it will return the expected answer
decimal share = (18 * 100)/58;
Solved: working perfectly with me
int a = 375;
int b = 699;
decimal ab = (decimal)a / b * 100;

basic maths calculations in c# [duplicate]

This simple calculation is returning zero, I can't figure it out:
decimal share = (18 / 58) * 100;
You are working with integers here. Try using decimals for all the numbers in your calculation.
decimal share = (18m / 58m) * 100m;
18 / 58 is an integer division, which results in 0.
If you want decimal division, you need to use decimal literals:
decimal share = (18m / 58m) * 100m;
Since some people are linking to this from pretty much any thread where the calculation result is a 0, I am adding this as a solution as not all the other answers apply to case scenarios.
The concept of needing to do calculations on various types in order to obtain that type as a result applies, however above only shows 'decimal' and uses it's short form such as 18m as one of the variables to be calculated.
// declare and define initial variables.
int x = 0;
int y = 100;
// set the value of 'x'
x = 44;
// Results in 0 as the whole number 44 over the whole number 100 is a
// fraction less than 1, and thus is 0.
Console.WriteLine( (x / y).ToString() );
// Results in 0 as the whole number 44 over the whole number 100 is a
// fraction less than 1, and thus is 0. The conversion to double happens
// after the calculation has been completed, so technically this results
// in 0.0
Console.WriteLine( ((double)(x / y)).ToString() );
// Results in 0.44 as the variables are cast prior to calculating
// into double which allows for fractions less than 1.
Console.WriteLine( ((double)x / (double)y).ToString() );
Because the numbers are integers and you perform integer division.
18 / 58 is 0 in integer division.
Whenever I encounter such situations, I just upcast the numerator.
double x = 12.0 / 23409;
decimal y = 12m / 24309;
Console.WriteLine($"x = {x} y = {y}");
double res= (firstIntVar * 100f / secondIntVar) / 100f;
when dividing numbers I use double or decimal , else I am getting 0 , with this code even if firstIntVar && secondIntVar are int it will return the expected answer
decimal share = (18 * 100)/58;
Solved: working perfectly with me
int a = 375;
int b = 699;
decimal ab = (decimal)a / b * 100;

C# Round up and down

I'm counting the results of my database.
If it's lower then 50, i want to divide them by 2.
Example:
if(CountResults < 50)
{
//CountResults = 39
int divided = CountResults / 2; //Results in 19
}
What i want:
if(CountResults < 50)
{
//CountResults = 39
int divided = CountResults / 2; //Results in 19,5
Math.Round(divided, 0);
}
I want to be able to round it upwards and down.
So i get the result 19.5 twice. Once i want it to be 19, and once to be 20..
How do i achieve this?
It's not clear how you are going to use your code twice, but if you want to divide integer into two integer parts just subtract first result from totals:
if(CountResults < 50)
{
//CountResults = 39
int divided1 = CountResults / 2; // 19
int divided2 = CountResults - divided1; // 20
}
First result will use integer division and it will give you result rounded towards zero (19 in your case). Further reading: C# Specification 7.7.2 Division Operator
Second result will give you rest which will be either equal to first result (if there was no rounding), or it will be equal to division rounded from zero (20 in your case).
The rounding part can be accomplished using these 2 nice methods:
Math.Floor brings it to the floor
Math.Celing lifts it to the celing ;)
The calculation part is a little more tricky. This statement:
int divided = CountResults / 2; //Results in 19,5
cannot really be true, or let's say it does not matter what is behind the comma because when it is assigned to the variable int devided it will loose this information and no rounding is anymore required.
When you want a result of type double (meaning e.g. 19,5 ) and you want to round that result, you need at least one of the parameters of the calculation to be of type double double!
Example
double var1 = 39;
int res_low = (int)Math.Floor(var1 / 2);
int res_high = (int)Math.Ceiling(var1 / 2);
note that writing 2 is implicitly seen by the compiler as int and writing 2.0 is implicitly seen as double. So this would yield the same result:
int var2 = 39;
int res_low2 = (int)Math.Floor(var2 / 2.0);
int res_high2 = (int)Math.Ceiling(var2 / 2.0);

Double? will trim decimals [duplicate]

Alright, I'm trying to calculate the percentage of two values. This should be really simple but for some weird reason it's not working. I'm too tired/dumb to figure it out.
Here's my code, it keeps returning 0, i checked the values while debugging and with FilesCompleted being 295 and TotalFilesCount being 25002 the returnvalue var is just 0, it should be 1 already.
private int CalculatePercentComplete(int FilesCompleted, int TotalFilesCount)
{
int returnvalue = (FilesCompleted / TotalFilesCount) * 100;
if (returnvalue > 100 || returnvalue < 1) return 1;
else return returnvalue;
}
i checked the values while debugging and with FilesCompleted being 295 and TotalFilesCount being 25002 the returnvalue var is just 0, it should be 1 already.
No, because all the arithmetic is being done with integers. So first this expression is evaluated:
(FilesCompleted / TotalFilesCount)
That's 295 / 25002. The result of that integer arithmetic is 0... and when you then multiply it by 100, you've still got 0. The simplest fix is just to do the multiplication first:
int returnvalue = (FilesCompleted * 100) / TotalFilesCount;
Note that that will overflow if FilesCompleted is greater than int.MaxValue / 100. You could fix that by either doing everything in floating point arithmetic:
int returnvalue = (int)((FilesCompleted * 100.0) / TotalFilesCount);
... or by using long integer arithmetic:
int returnvalue = (int)((FilesCompleted * 100L) / TotalFilesCount);
Neither of these are necessary if you don't expect to have an insane number of files, of course. (You're fine up to 42 million files...)
As a side note, your parameter names violate .NET naming conventions. They should be camelCased - totalFilesCount and filesCompleted.
How about
int returnvalue = (int)(((double)FilesCompleted / TotalFilesCount) * 100);
What this is doing
Converting int FilesCompleted to double. For eg if its 295, then this will convert it into 295.0 so that division happens in double.
There is no need to convert TotalFilesCount to double also as divison of double by integer (or integer by double) returns a double.
So the returned double result is 0.011799056075513958 which is multipled by 100
So the retunred result is 1.1799056075513958 which is finally converted to int which returns 1
The FilesCompleted/TotalFilesCount returns 0.01 which in int format is 0, try FilesCompleted*100/TotalFilesCount!
Indeed, simple error, when doing diversions with int, the answer will be an int. Your answer would be between 0 and 1 so rounded down it is 0 (int).
Use:
int returnvalue = (int)((FilesCompleted / (double)TotalFilesCount) * 100);
To make your calculation use a double for the fractional number and then multiply that by 100 and cast that to an int.

Categories