68

This simple calculation is returning zero, I can't figure it out:

decimal share = (18 / 58) * 100;
Andrew
  • 18,680
  • 13
  • 103
  • 118
Zo Has
  • 12,599
  • 22
  • 87
  • 149

8 Answers8

87

You are working with integers here. Try using decimals for all the numbers in your calculation.

decimal share = (18m / 58m) * 100m;
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Daniel Lee
  • 7,709
  • 2
  • 48
  • 57
  • 2
    It is not inherently clear why the literal "18" is compiled into an integer. To clarify, per the language specification (Lexical structure > Tokens > Literals) any literal written using the "decimal digits" 0 through 9 are [Integer Literals](https://msdn.microsoft.com/en-us/library/aa664674(v=vs.71).aspx) which compile into "the first of these types in which its value can be represented: `int`, `uint`, `long`, `ulong`.", once you add the "m" suffix the literal is now seen as a [Real Literal](https://msdn.microsoft.com/en-us/library/aa691085(v=vs.71).aspx) which compiles as type `decimal`. – Quantic Jan 24 '17 at 16:49
  • Example: `var check = (1/(decimal)7)*100 = 14.285714 ` casting the denominator to decimal will solve the problem. – Pranesh Janarthanan Dec 09 '17 at 06:53
28

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;
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
22

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() );
Kraang Prime
  • 9,981
  • 10
  • 58
  • 124
4

Because the numbers are integers and you perform integer division.

18 / 58 is 0 in integer division.

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
3

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}");
pacholik
  • 8,607
  • 9
  • 43
  • 55
adonthy
  • 151
  • 2
  • 6
1
 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

aris
  • 409
  • 6
  • 8
  • 1
    Welcome to Stack Overflow. Code dumps without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please [edit] your question and explain how it answers the specific question being asked. See [answer]. This is particularly important when answering old questions (this one is over 9 years old) with existing answers. How does this answer improve upon what's already here? – ChrisGPT was on strike Apr 19 '21 at 19:05
0

Solved: working perfectly with me

  int a = 375;
  int b = 699;
  decimal ab = (decimal)a / b * 100;
Abdul Khaliq
  • 2,139
  • 4
  • 27
  • 31
0

decimal share = (18 * 100)/58;

Prabhavith
  • 458
  • 1
  • 4
  • 15