Share via


Dividing smaller number by a larger number yields a 0?

Question

Tuesday, January 24, 2012 9:28 PM

why do I get a 0 when I try 

decimal temp = 1061 / 1599;

All replies (2)

Tuesday, January 24, 2012 9:36 PM ✅Answered | 2 votes

this worked

 

decimal temp = (decimal)1061 / (decimal) 1599;


Tuesday, January 24, 2012 9:39 PM ✅Answered | 4 votes

Because 1061 and 1599 are both considered integer, so .NET perform an integer division.

To obtain also the decimal part of the division, the operands must be decimal (to tell the truth, at least one of them must). It this way, .NET will perform a decimal division:

decimal temp = (decimal)1061 / (decimal)1599;

 Now temp will contain the real value.

Marco Minerva [MCPD]
Blog: http://blogs.ugidotnet.org/marcom
Twitter: @marcominerva