Divide 2 long values and get a decimal result

Posted on 2007-10-02
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
    LVL 9

    Accepted Solution

    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


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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now