This question already has answers here:
Extremely basic division equation not working in c#
(3 answers)
Closed 9 years ago.
(5 / 15) * 1185 should give 395.
decimal Test = (5 / 15) * 1185;
This, however, returns 0. What am I doing wrong?
5 / 15 is an integer division and returns 0. Use 5m / 15m to force decimal.
m is the C# suffix to signify to the compiler that the number your wrote is a decimal, even if it looks like an int. You can use f for floats and d for doubles (or 5.0).
While on the topic of suffixes, there is also L for long but that wouldn't have helped your with your division because it is also an integer type.
5 // Int32
5L // Int64
5d // Double
5.0 // Double
5m // Decimal
5f // Single
You need to use floating point division, not integer division. You can get the correct result by making it a floating point number, or using m (to specify it as a decimal) behind the 15:
decimal Test = (5 / 15.0) * 1185;
or
decimal Test = (5 / 15m) * 1185;
You could also use a lambda expression :
Func<decimal, decimal, decimal, decimal> exp = (x, y, z) => (x / y) * z;
Console.WriteLine(exp(5, 15, 1185));
outputs:
394.999999
as you are expecting 395 you will need to use a Math.Round
Console.WriteLine(Math.Round(exp(5, 15, 1185), 1));
outputs: 395.0
5/15 is 0 because you are using integer division.
You can try use one of the following code:
decimal test = (5m / 15) * 1185
decimal test = ((decimal)5 / 15) * 1185
decimal test = (5.0 / 15) * 1185
The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593,543,950,335 to negative 79,228,162,514,264,337,593,543,950,335. The Decimal type does not eliminate the need for rounding but rather minimizes errors due to rounding.
Link: http://msdn.microsoft.com/en-us/library/364x0z75
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 question already has answers here:
Why do these division equations result in zero?
(10 answers)
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 4 years ago.
I'm calculating the percentage to add in a datagrid, but when I report the data it always returns me 0, what am I doing wrong?
if I have the following case: the variable quantidadeInstalada has the value of 10 and the goal has 20 the concluido would have to return me 50 but it returns me 0
private void AdicionarPessoa()
{
string Valida = ValidaPessoa();
if (Valida.Equals(""))
{
double concluido=0, falta=0;
int quantidadeInstalada = Convert.ToInt32(ttbQuantidade.Text);
int meta = Convert.ToInt32(ttbMetaPessoa.Text);
concluido = (quantidadeInstalada/meta)*100;
falta = 100-concluido;
MessageBox.Show(concluido.Text);
}
else
MessageBox.Show(Valida);
}
It's probably due to the precision of the int. Use a decimal or double instead.
When we use an integer, we lose precision.
Console.WriteLine(100 / 17); // 5
Console.WriteLine(100 / 17m); // 5.8823529411764705882352941176
Console.WriteLine(100 / 17d); // 5.88235294117647
Console.WriteLine(100 / 17f); // 5.882353
Since integers always round down, 0.99 as an integer is 0.
Note that for precision, the types of the inputs matters.
double output = input1 * input2;
For example:
double outputA = 9 / 10;
Console.WriteLine(outputA); // 0
double outputB = 9 / 10d;
Console.WriteLine(outputB); // 0.9
double outputC = 9d / 10;
Console.WriteLine(outputC); // 0.9
Here is a Fiddle.
It's because you're doing integer division. You can fix it by casting one of the variables to a double like this:
concluido = ((double)quantidadeInstalada/meta)*100;
When you're dividing int by int (as in "quantidadeInstalada/meta"), you'll get an int. Either use a fractional type (e.g. decimal, double) from the very beginning, as Shaun suggested, or (if the integral types have to stay), cast the values to a fractional type in the division expression (as itsme86 shown).
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;
When I try to do this
double test = ((2 / 7) * 100);
it returns 0.
Does anybody know why this is and how to work around it?
Thanks
2 / 7 is integer division, and will return 0. Try this instead
2.0 / 7
(double) 2 / 7
You're dividing integers.
If you want a non-integer result, at least one operand must be a float or double (or decimal).
You can do that by adding .00 to any of the literals to create a literal.
You are dividing integers, so 2 / 7 becomes already 0. Just try 2.0 / 7.0 and you'll get the correct result.
It's doing integer division because all the operands are integers.
To fix it, change at least one the operands to doubles like this:
double test = ((2.0 / 7.0) * 100.0);
You are doing integer math, and only converting to double when you have the final result.
2 / 7 = 0
while
2.0 / 7.0 = 0.285714285714285
Do the math with double values:
double test = ((2.0 / 7.0) * 100.0);
It's because of division. A division of two int numbers returns int number truncating any decimal points. Hence the result of the operation 2/7 will be 0.
It should be something like this:
double test = ((2.0 / 7.0) * 100.0);
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;