【问题标题】:Division returns zeroDivision returns zero
【发布时间】:2021-08-24 18:07:22
【问题描述】:

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

decimal share = (18 / 58) * 100;

【问题讨论】:

    标签: c#


    【解决方案1】:

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

    decimal share = (18m / 58m) * 100m;
    

    【讨论】:

    • 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 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 which compiles as type decimal.
    • Example: var check = (1/(decimal)7)*100 = 14.285714 casting the denominator to decimal will solve the problem.
    【解决方案2】:

    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;
    

    【讨论】:

    • Well that is new to me I was just following my calculator.
    【解决方案3】:

    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() );
    

    【讨论】:

      【解决方案4】:

      Because the numbers are integers and you perform integer division.

      18 / 58 is 0 in integer division.

      【讨论】:

        【解决方案5】:

        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}");
        

        【讨论】:

          【解决方案6】:
           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

          【讨论】:

          • Welcome to Stack Overflow. Code dumps without any explanation are rarely helpful. Stack Overflow is about learning, not providing sn-ps to blindly copy and paste. Please edit your question and explain how it answers the specific question being asked. See How to 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?
          【解决方案7】:

          decimal share = (18 * 100)/58;

          【讨论】:

            【解决方案8】:

            Solved: working perfectly with me

              int a = 375;
              int b = 699;
              decimal ab = (decimal)a / b * 100;
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2022-12-02
              • 2023-02-05
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-11-17
              相关资源
              最近更新 更多