Divide 2 long values and get a decimal result

Posted on 2007-10-02
Medium Priority
Last Modified: 2013-12-17
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?
Question by:rajesh_khater

Accepted Solution

jonorossi earned 2000 total points
ID: 19999731
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;
LVL 11

Expert Comment

ID: 20001034

decimal n3 = (decimal) (n1 / n2);

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question