158

Forgive me if this is a naïve question, however I am at a loss today.

I have a simple division calculation such as follows:

double returnValue = (myObject.Value / 10);

Value is an int in the object.

I am getting a message that says Possible Loss of Fraction. However, when I change the double to an int, the message goes away.

Any thoughts on why this would happen?

1
  • 1
    Thank you all for the great responses. It makes sense now that you lose the decimal point when dividing 2 int values. Commented Jun 30, 2009 at 1:30

5 Answers 5

210

When you divide two int's into a floating point value the fraction portion is lost. If you cast one of the items to a float, you won't get this error.

So for example turn 10 into a 10.0

double returnValue = (myObject.Value / 10.0);
Sign up to request clarification or add additional context in comments.

Comments

66

You're doing integer division if myObject.Value is an int, since both sides of the / are of integer type.

To do floating-point division, one of the numbers in the expression must be of floating-point type. That would be true if myObject.Value were a double, or any of the following:

double returnValue = myObject.Value / 10.0;
double returnValue = myObject.Value / 10d; //"d" is the double suffix
double returnValue = (double)myObject.Value / 10;
double returnValue = myObject.Value / (double)10;

Comments

10

Assuming that myObject.Value is an int, the equation myObject.Value / 10 will be an integer division which will then be cast to a double.

That means that myObject.Value being 12 will result in returnValue becoming 1, not 1.2.

You need to cast the value(s) first:

double returnValue = (double)(myObject.Value) / 10.0;

This would result in the correct value 1.2, at least as correct as doubles will allow given their limitations but that's discussed elsewhere on SO, almost endlessly :-).

Comments

7

An integer divided by an integer will return your an integer. Cast either Value to a double or divide by 10.0.

Comments

4

I think since myObject is an int, you should

double returnValue=(myObject.Value/10.0); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.