reiley
reiley

Reputation: 3761

Java: Issue in converting result of exponential values to int

Suppose I have a range from 0-100 & I choose a random number X say 29.

Now I increase the range to a big number say a 10-digit 1023654740.

Now I want to find the place of that X in 0-1023654740. (so that is I belive is 29% of 1023654740)

If I perform the calculation using Double, I'm getting an exponent value.

double range = 1023654740;
double position = 29;   // Can be any number from 0 - range
Double val = (((double) position / range) * 100);

Result: 2.8329864422842414E-6

But I want final result in int.(dont care if the final value is rounded off or truncated)

Any suggestions

Upvotes: 0

Views: 323

Answers (3)

YMomb
YMomb

Reputation: 2387

If it is a matter of presenting the result you should have a look at:

String myBeautifulInteger = NumberFormat.getIntegerInstance().format(val);

If it is just a matter of having an integer.

int myInt = val.intValue();

Upvotes: 1

ekholm
ekholm

Reputation: 2573

First, your calculation is wrong. According to your description, you probably want to do the following:

Double val = ((double)position / 100) * range;

Then you can get your int value (by truncation) through:

int intVal = val.intValue();

Upvotes: 1

user902383
user902383

Reputation: 8640

as if your result is Double why you just dont do

val.intValue()

Upvotes: 1

Related Questions