cullub - 1 year ago 77
C# Question

# High(ish) presision math on decimal

## The Question:

I am using Visual C# Express 2010. I am trying to divide three integers, however, the result is always 0.

My Code:

``````//earlier in the code:
int totalKeywords = 3;

//the problem code:
decimal onePercent = 100 / totalKeywords / 100;  //100% divided by the number of keywords divided by 100 to make one percent

MessageBox.Show(onePercent);

//result: 0
//what I want: 0.33 or more acurate
``````

What I've tried:

• I've changed the value of totalKeywords

• I've tried onePercent as a double, int, float, ect.

Guesses:

• It could be that the built-in math doesn't work for some reason (WHY??)

• It could be that decimal / int / float, etc. don't hold decimals (I don't think so)

My Efforts:

• Stack Overflow

Suggestions are accepted, and any help will be appreciated!

Thank you!

The Problem:

I was using Integer Math - I was doing math on only Integers, so when I assigned it to a decimal, the result was 0.

The Solution:

Affix an
`m`
to the end of one of the Integers to make it a decimal.

My Final Code:

``````//earlier in the code:
int totalKeywords = 3;

//the problem code:
decimal onePercent = 100m / totalKeywords / 100;  //note the "m" affixed to the first 100

MessageBox.Show(onePercent);

//result: 0.33
``````

Let's break it down:

``````decimal onePercent = 100 / totalKeywords / 100;
``````

First, divide the integer literal 100 by the integer variable totalKeywords (value is 3). Result: integer 33.

Next, divide the result 33 by the integer literal 100. Result: integer 0.

The right-hand expression has type int, value 0. Convert that implicitly to the decmal 0m, so you can then assign that to the decimal variable onePercent.

Result: 0m.

To fix, as others have noticed, make the leftmost constant (if not all of them, for clarity) into a decimal. This will do, as the ints will implicitly convert to decimal:

``````decimal onePercent = 100m / totalKeywords / 100;
``````

This is totally unambiguous, if a little over the top:

``````decimal onePercent = 100m / (decimal)totalKeywords / 100m;
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download