hitesh
hitesh

Reputation: 499

divide 2 decimal numbers where the result is known to be int

Hi I am trying to divide 2 very large double numbers and result is known to be int only. I am getting result either 1 less or 1 more if I use Math.ceil or cast to int. What is the best way to do this.

I have tried type casting and Math.ceil function.

double num1=sum-totalsum;
double d=(num1*totalmul);

double diff=mul-totalmul;
double missing=d/diff;
double repeated=missing+num1;
System.out.println((int)Math.ceil(repeated)+" "+(int)Math.ceil(missing));

expected result is int. repeated and missing are always int.

Upvotes: 0

Views: 52

Answers (1)

enerve
enerve

Reputation: 63

I believe what you're looking for is Math.round()

So:

System.out.println((int)Math.round(repeated)+" "+(int)Math.round(missing));

Upvotes: 1

Related Questions