简体   繁体   English

Java:将指数值的结果转换为int的问题

[英]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. 假设我的范围是0-100 ,我选择了一个随机数X,例如29。

Now I increase the range to a big number say a 10-digit 1023654740. 现在,我将范围扩大到一个很大的数字,例如10位数1023654740。

Now I want to find the place of that X in 0-1023654740. 现在,我想在0-1023654740中找到该X的位置。 ( so that is I belive is 29% of 1023654740 ) 所以我相信是1023654740的29%

If I perform the calculation using Double , I'm getting an exponent value. 如果我使用Double执行计算,则会得到指数值。

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 . 但是我想要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值(通过截断):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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