11

The result of all of the division equations in the below for loop is 0. How can I get it to give me a decimal e.g.:

297 / 315 = 0.30793650793650793650793650793651

Code:

using System;

namespace TestDivide
{
    class Program
    {
        static void Main(string[] args)
        {

            for (int i = 0; i <= 100; i++)
            {
                decimal result = i / 100;
                long result2 = i / 100;
                double result3 = i / 100;
                float result4 = i / 100;
                Console.WriteLine("{0}/{1}={2} ({3},{4},{5}, {6})", i, 100, i / 100, result, result2, result3, result4);
            }
            Console.ReadLine();
        }
    }
}

Answer:

Thanks Jon and everyone, this is what I wanted to do:

using System;

namespace TestDivide
{
    class Program
    {
        static void Main(string[] args)
        {
            int maximum = 300;

            for (int i = 0; i <= maximum; i++)
            {
                float percentage = (i / (float)maximum) * 100f;
                Console.WriteLine("on #{0}, {1:#}% finished.", i, percentage);
            }
            Console.ReadLine();
        }
    }
}
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
  • Possible duplicate of [C# is rounding down divisions by itself](https://stackoverflow.com/questions/6311855/c-sharp-is-rounding-down-divisions-by-itself) – user202729 Jun 25 '18 at 14:47
  • I know that this is not the point of the question... but I think that 297.0 / 315 is 0.942857142857143 rather than 0.30793650793650793650793650793651. :) – headsling Jul 30 '09 at 11:35

10 Answers10

31

You're using int/int, which does everything in integer arithmetic even if you're assigning to a decimal/double/float variable.

Force one of the operands to be of the type you want to use for the arithmetic.

for (int i = 0; i <= 100; i++)
{
    decimal result = i / 100m;
    long result2 = i / 100;
    double result3 = i / 100d;
    float result4 = i / 100f;
    Console.WriteLine("{0}/{1}={2} ({3},{4},{5}, {6})", 
                      i, 100, i / 100d, result, result2, result3, result4);
}

Results:

0/100=0 (0,0,0, 0)
1/100=0.01 (0.01,0,0.01, 0.01)
2/100=0.02 (0.02,0,0.02, 0.02)
3/100=0.03 (0.03,0,0.03, 0.03)
4/100=0.04 (0.04,0,0.04, 0.04)
5/100=0.05 (0.05,0,0.05, 0.05)

(etc)

Note that that isn't showing the exact value represented by the float or the double - you can't represent 0.01 exactly as a float or double, for example. The string formatting is effectively rounding the result. See my article on .NET floating binary point for more information as well as a class which will let you see the exact value of a double.

I haven't bothered using 100L for result2 because the result would always be the same.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
11

Try

i / 100.0
Lloyd
  • 1,324
  • 7
  • 10
  • This answer would get an upvote from me if it also explained *why* this would work ;o) – Fredrik Mörk Jul 30 '09 at 09:38
  • Code from the questions doesn't work because "/"-operator being called with integer arguments does integer division.If any of arguments is float or double, then "/" does the division you expect. :0) – Lloyd Apr 13 '11 at 12:38
5

because i is an int: i / 100 performs integer division, then the result, that is always 0, is casted to the target type. You need to specify at least one non-int literal in your expression:

i / 100.0 
dfa
  • 114,442
  • 31
  • 189
  • 228
2

Because i is an integer and 100 is an integer...so you have an integer division

Try (decimal)i / 100.0 instead

Scoregraphic
  • 7,110
  • 4
  • 42
  • 64
1

No matter where you store it, an integer divided by an integer will always be an integer.

lotsoffreetime
  • 1,100
  • 5
  • 11
1

You need to force a floating point operation "double / double" instead of an "int / int"

double result = (double)297 / (double)315 ;
Tormod
  • 4,551
  • 2
  • 28
  • 50
0

Because i is a int value and you divide by an integer so the result is an integer ! and so you need to divide by 100.0 to have an implicit cast in float or specify 100f or 100d

Matthieu
  • 2,743
  • 19
  • 21
0

this is integer division whatever the type of variable you storing in, so int / int = int

Ahmed
  • 7,148
  • 12
  • 57
  • 96
0
double result3 = ((double)i) / 100;
Steve Gilham
  • 11,237
  • 3
  • 31
  • 37
0

In my case I had only vars and no int

float div = (var1 - var2) / float.Parse(var1.ToString());
Yohan Dahmani
  • 1,670
  • 21
  • 33