简体   繁体   中英

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

好像您的结果是Double为什么您不这样做

val.intValue()

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();

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM