Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why are results rounding down ? (C#)

I’m a beginner trying to make a very basic calculator.
I have a problem with division, though;
For some reasons, the result is always rounded down not matter what:

Console.WriteLine("First Argument ?");
int Arg1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Second argument ?");```
int Arg2 = Convert.ToInt32(Console.ReadLine());
float OpDiv = Arg1 / Arg2;
Console.WriteLine($"The result is {OpDiv}");

For example here, 11 / 3 returns 3, instead of 3.66666666…
What have I done wrong ???

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Change to:

        Console.WriteLine("First Argument ?");
        int Arg1 = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Second argument ?");
        int Arg2 = Convert.ToInt32(Console.ReadLine());
        float OpDiv = (float)Arg1 / Arg2;
        Console.WriteLine($"The result is {OpDiv}");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading