We are excited to announce that the ASP.NET Forums are moving to the new Microsoft Q&A experience. Learn more >

# How to divide an integer by an integer and yield a decimal RSS

## 18 replies

Last post Feb 06, 2018 09:36 AM by HTMLWarrior

• thuhue

Contributor

2109 Points

2884 Posts

### How to divide an integer by an integer and yield a decimal

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.

• jimski3000

Member

30 Points

294 Posts

### Re: How to divide an integer by an integer and yield a decimal

Convert the integers to decimal: ```int i = 77; int j = 21; decimal d = (decimal)i / (decimal)j; ```Jim ThoughtWorks
• thuhue

Contributor

2109 Points

2884 Posts

### Re: How to divide an integer by an integer and yield a decimal

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.

• KraGiE

Member

90 Points

2762 Posts

### Re: How to divide an integer by an integer and yield a decimal

This is just saying for hell of saying, but you *could* always overload the \ operator. *grin*
The easiest day was yesterday.
• jimski3000

Member

30 Points

294 Posts

### Re: How to divide an integer by an integer and yield a decimal

"you *could* always overload the \ operator" Overloads are attached to types, though. You can only overload operators on types that you control! Jim ThoughtWorks
• jimski3000

Member

30 Points

294 Posts

### Re: How to divide an integer by an integer and yield a decimal

"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
• imleggy

None

0 Points

507 Posts

### Re: How to divide an integer by an integer and yield a decimal

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
Dave Legg
• thuhue

Contributor

2109 Points

2884 Posts

### Re: How to divide an integer by an integer and yield a decimal

I found it: myDecimal.ToString("N2"); Thanks.

• pickyh3d

Participant

1203 Points

1896 Posts

### Re: How to divide an integer by an integer and yield a decimal

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?
Picky
• thuhue

Contributor

2109 Points

2884 Posts

### Re: How to divide an integer by an integer and yield a decimal

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.

• KraGiE

Member

90 Points

2762 Posts

### Re: How to divide an integer by an integer and yield a decimal

::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.
The easiest day was yesterday.
• pickyh3d

Participant

1203 Points

1896 Posts

### Re: How to divide an integer by an integer and yield a decimal

I realize the casting was done to retain the decimal amount, but I think a double would suffice.
Picky
• KraGiE

Member

90 Points

2762 Posts

### Re: How to divide an integer by an integer and yield a decimal

22/7... put that in a double. :)
The easiest day was yesterday.
• jimski3000

Member

30 Points

294 Posts

### Re: How to divide an integer by an integer and yield a decimal

"you can always overload the / for int,int to return a decimal type" And how would you do that? Jim ThoughtWorks
• pickyh3d

Participant

1203 Points

1896 Posts

### Re: How to divide an integer by an integer and yield a decimal

Kragie, they just want two decimal places.
Picky
• KraGiE

Member

90 Points

2762 Posts

### Re: How to divide an integer by an integer and yield a decimal

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.
The easiest day was yesterday.
• thuhue

Contributor

2109 Points

2884 Posts

### Re: How to divide an integer by an integer and yield a decimal

I'm interested in examples on overloads of operators. Please kindly provide some. Thanks.

• Jay Raj

None

0 Points

1 Post

### Re: How to divide an integer by an integer and yield a decimal

Sep 21, 2017 06:00 PM|Jay Raj|LINK

I tried this, works for C#

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

• HTMLWarrior

None

0 Points

3 Posts

### Re: How to divide an integer by an integer and yield a decimal

```int i = 77;