I am facing a problem in dividing numbers in c#.
See my code in C# for division
double openRate = 0,
long a=542;
long b=4795;
openRate =(a/b)*100
This gives 11.303.. in my calculator .
But c# gives me 0.0
What could be reason?
When you write
long a = 542;
long b = 4795;
Since because a / b is calculated as an integral value; any fractional part was dropped. So a / b is equal 0 at this point not 0,113...
From elemantary school math;
0 * 100 = 0
Your calculator use probably floating division so actually it calculates this like;
double openRate = 0;
long a = 542;
long b = 4795;
openRate =((double)a / b) * 100; // 11.303...
a and b are integers and get divided using the operator/ of long, resulting in an integral division.
542 / 4795 = 0.113
After this they got multiplied with 100, which is an integer, either.
0 * 100 = 0
Last but not least the (still integral) result get's converted into an double. What you want to write is something like this:
openRate = ((double)a / (double)b) * 100.0;
Related
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;
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;
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;
I understand if .NET rounds 2.5 to 2 using banker's rounding. But, how this could be:
decimal point;
point =51 * 70 / 100;
Math.Round(point,0, MidPointRounding.AwayFromZero);
rounds to 35?
How can I make all .5's round to upper integer even if it's odd?
This second line in your snippet already gives you an integer result.
51, 70, 100 are of type int, therefore the operators for integer multiplication and division are chosen. The result of an integer multiplication or division is always of type integer again, and possible decimal places are truncated when dividing using / on integers.
The statement point = 51 * 70 / 100; is equivalent to
int tmp = 51 * 70; // result is 3570
tmp = tmp / 100; // result is 35 (!!!)
point = (decimal)tmp; // point is 35m;
The solution is to change your code so that it uses decimal arithmetic:
point = 51m * 70m / 100m; // point is 35.7m
Actually it is sufficient that one of the operands is of type decimal. This can either be achieved by using the suffix m (for monetary) or by using a type cast. The following sample will also give the desired result:
point = (decimal)51 * 70 / 100;
You are doing integer division. Try this instead:
decimal point = 51m * 70m / 100m;
I need to get two thirds and round to whole number.
Standard rounding:
.6 rounds to 1
.5 rounds to 1
.4 rounds to 0
AwayFromZero I believe is the right option to use here. But I think my multiplication is not right because I'm getting 0 for my result.
int totalPoints= 71625;
int twoThirds = (int)Math.Round((double)totalPoints * (2 / 3), 0, MidpointRounding.AwayFromZero);
Answer should be 47750.
Because 2/3 is an int calculation so it will return 0, then 0 * totalPoints equal 0
I would use 2/3m let the calculation result be decimal. then you can get your expectation result.
int totalPoints = 71625;
int twoThirds = (int)Math.Round(totalPoints * (2/3m), 0, MidpointRounding.AwayFromZero);
While the #D-Shih answer is correct here, it is not the best you can get. Even for decimal, two thirds are not representable exactly, 2/3m is not exactly two thirds, so rounding error occurs. When you multiply the number, you multiply the error too. It is better to make the dividing as last. Let me show the difference:
int totalPoints = 71625;
decimal result1 = totalPoints * (2 / 3m); //47750.000000000000000000000002M
decimal result2 = (decimal)totalPoints * 2 / 3; //47750 (exactly)
double result3 = (double)totalPoints * 2 / 3; //47750 (exactly)
not a big deal here, as the results would be the same. But if we alter the example a bit
double totalPointsDouble = 71626.5d;
decimal result1 = (decimal)totalPointsDouble * (1 / 3m); //23875.499999999999999999999998M
decimal result2 = (decimal)totalPointsDouble * 1 / 3m; //23875.5M
double result3 = totalPointsDouble * 1 / 3; //23875.5
int oneThird1 = (int)Math.Round((decimal)totalPointsDouble * (1 / 3m), 0, MidpointRounding.AwayFromZero);
//23875
int oneThird3 = (int)Math.Round(totalPointsDouble * 1 / 3, 0, MidpointRounding.AwayFromZero);
//23876
the rounding error propagates to the result and the results differ. According to math, 23876 is correct.
(2 / 3) is evaluated first because it is in parentheses. The result is 0 because this is an integer division.