0

I'm sure this is a stupid question but I'm a newbie!

namespace Rextester
{    
    public class Program
    {
        public static void Main(string[] args)
        {
            var myInt = ((1000/2048) * 1536);
            Console.WriteLine(myInt);
        }
    }
}

Output is: 0

Can someone tell me how to get the correct number (750)?

Lavinia N.
  • 246
  • 1
  • 4
  • 13
Peter J
  • 105
  • 1
  • 5
  • If you're just starting, I'd recommend using the microsoft docs as your first point of reference. The [Division operator](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/division-operator) page tells you that integer division produces integer results, and so you should be able to work out why this isn't working. – Damien_The_Unbeliever Jan 22 '18 at 08:23
  • If duplicate is not enough - see if other articles from search you've presumable done help - https://www.bing.com/search?q=c%23+integer+division+zero – Alexei Levenkov Jan 22 '18 at 08:36

1 Answers1

0

By default, 1000 and 2048 are int, therefore the result will be treated as an int. You have to cast them to double.

Option 1:

var myInt = (((double)1000 / 2048) * 1536);
Console.WriteLine(myInt);

Option 2:

var myInt = ((1000.0 / 2048) * 1536);
Console.WriteLine(myInt);

You should read about value types in C#:

https://www.tutorialspoint.com/csharp/csharp_data_types.htm

https://msdn.microsoft.com/en-us/library/system.double(v=vs.110).aspx

Lavinia N.
  • 246
  • 1
  • 4
  • 13
  • and to work with the Decimal type, you can cast the numbers by appending a `m` at the end : `1000m` – Pac0 Jan 22 '18 at 08:34