• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1971
  • Last Modified:

Divide 2 long values and get a decimal result

How can I divide 2 long values and get result in decimal?

long n1 = 7;
long n2 = 2;
decimal n3 = n1/n2;

I get n3 = 3, whereas it should be 3.5. Whats wrong and how to correct it?
0
rajesh_khater
Asked:
rajesh_khater
1 Solution
 
jonorossiCommented:
The "n1/n2" expression is being evaluated as being long / long. Therefore, you need to cast one to a decimal first so it knows that the divide is should result in a decimal.

decimal n3 = (decimal)n1 / n2;
0
 
udhayakumardCommented:
Try

decimal n3 = (decimal) (n1 / n2);
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now