locked
How to divide an integer by an integer and yield a decimal RRS feed

  • Question

  • User-863835478 posted
    If we divide an integer by another integer, the result in C# is always an integer. How can we make the result to be 2 decimal? For example: 77 / 21 = 3.67 Thanks.
    Tuesday, August 24, 2004 9:35 PM

All replies

  • User122375535 posted
    Convert the integers to decimal: int i = 77; int j = 21; decimal d = (decimal)i / (decimal)j; Jim ThoughtWorks
    Wednesday, August 25, 2004 7:16 AM
  • User-863835478 posted
    Thanks, that's what I found too. But the decimal needs to be formatted to 2 decimal. Otherwise it would show as a long bunch of decimals. Specifically how do we format a long decimal to "999.99" in C#? (We know how to do it in VB) Thanks.
    Wednesday, August 25, 2004 1:18 PM
  • User-967169866 posted
    This is just saying for hell of saying, but you *could* always overload the \ operator. *grin*
    Wednesday, August 25, 2004 5:28 PM
  • User122375535 posted
    "you *could* always overload the \ operator" Overloads are attached to types, though. You can only overload operators on types that you control! Jim ThoughtWorks
    Wednesday, August 25, 2004 5:59 PM
  • User122375535 posted
    "how do we format a long decimal to "999.99"" Use the ToString() overload with a format specifier. Something like (off the top of my head): myDecimal.ToString("#.00"); Jim ThoughtWorks
    Wednesday, August 25, 2004 6:01 PM
  • User-1732136300 posted
    Also look at Math.Round, unfortunatly i think it only supports the double type in version 1.1 and decimal support is added in v2 Dave Legg
    Friday, August 27, 2004 4:43 AM
  • User-863835478 posted
    I found it: myDecimal.ToString("N2"); Thanks.
    Friday, August 27, 2004 5:26 PM
  • User743145481 posted
    To be honest, isn't the decimal type overkill if you are going for two places of precision when dividing an integer by an integer?
    Sunday, August 29, 2004 4:13 AM
  • User-863835478 posted
    What we did here was for getting a decimal when one integer dividing another yields a decimal value such as 7/3=2.33, not a rounded integer. Thanks.
    Sunday, August 29, 2004 5:35 PM
  • User-967169866 posted
    ::Overloads are attached to types, though. You can only overload operators on types that you control! Jim, you're right, but you can control the output. you can return a different type. you can always overload the / for int,int to return a decimal type, and do the casting in the overloaded operator.
    Sunday, August 29, 2004 6:46 PM
  • User743145481 posted
    I realize the casting was done to retain the decimal amount, but I think a double would suffice.
    Sunday, August 29, 2004 9:15 PM
  • User-967169866 posted
    22/7... put that in a double. :)
    Monday, August 30, 2004 3:03 AM
  • User122375535 posted
    "you can always overload the / for int,int to return a decimal type" And how would you do that? Jim ThoughtWorks
    Monday, August 30, 2004 3:49 PM
  • User743145481 posted
    Kragie, they just want two decimal places.
    Monday, August 30, 2004 7:14 PM
  • User-967169866 posted
    Jim, I didn't say it's going to be easy. Or that it was a good solution. I just said it was "possible". Just create an object that is essentially a decimal, and start creating a crapload of overloaded operators. Now. I'll admit that the original post was done in bad taste for humor, but still. It "is" possible. I'd be the first to slap someone that wanted to do it like that, but that wasn't the purpose of my initial post here.
    Tuesday, August 31, 2004 2:48 AM
  • User-863835478 posted
    I'm interested in examples on overloads of operators. Please kindly provide some. Thanks.
    Tuesday, August 31, 2004 10:57 AM
  • User1341108456 posted

    I tried this, works for C#

    int i = 77;
    int j = 21;
    double d = decimal.ToDouble(i) / j;
    Console.WriteLine("{0:.00}",d);
    Console.ReadLine();

    Thursday, September 21, 2017 6:00 PM
  • User1718151756 posted
    int i = 77;
    int j = 21;
                
    // convert int to decimal
    decimal di = Convert.ToDecimal(i);
    decimal dj = Convert.ToDecimal(j);
    
    // compute 
    decimal result = di / dj;
    
    // display the result
    Console.WriteLine("{0} / {1} = {2}", di, dj, result.ToString("n2"));

    Tuesday, February 6, 2018 9:36 AM