简体   繁体   English

将float转换为int

[英]Convert a float to int

Android Studio 0.5.2

Hello, 你好,

I have a random float value that I want to convert to an int. 我有一个随机的float值,我想将其转换为int。

The float could have a random number between -9.4182062E-9 to 9.593071E-8 . 浮点数可以在-9.4182062E-99.593071E-8之间具有随机数。

I just want to get the positive int number so if the number is negative ie -5.5115866E-8 it should just return 5. I have used the Math.abs for this. 我只想获取正整数值,所以如果数字为负数,即-5.5115866E-8它应该只返回5。为此,我使用了Math.abs。

I have tried the following: 我尝试了以下方法:

int accel =  (int)Math.abs(randomAccel);

However, accel keeps giving me a zero no matter what value randomAccel is. 但是,无论randomAccel是什么值, accel总是给我零。

Many thanks for any suggestions, 非常感谢您的任何建议,

Your negative number -5.485747E-5 finish by E-5 meaning that it is -0.0000548574 So the absolute value is 0.0000548574 . 您的负数-5.485747E-5E-5 -0.0000548574 ,表示它是-0.0000548574因此绝对值为0.0000548574

So Math.abs(-0.0000548574) is 0.0000548574 所以Math.abs(-0.0000548574)0.0000548574

(int)0.0000548574 is 0 (int)0.00005485740

result is 0 结果是0

Are your sure your code is responding 0 no matter the value of random? 您确定代码是否响应0无论random的值如何? Check the number you are trying to convert. 检查您要转换的号码。

You should unit test your code with your values. 您应该使用您的值对代码进行单元测试。

Your random number range has very small a magnitude (negative exponents) - importantly it is less than +/-1 . 您的随机数范围的幅度非常小(负指数),重要的是它小于+/-1 When float is cast to int , all fractional parts are truncated , thus every number in your range, after setting the sign to positive, will result in 0 if cast to int . floatint ,所有小数部分都会被截断 ,因此将符号设置为正数后,范围内的每个数字如果转换为int都将得出0

It seems like you want to ignore the exponent of your numbers. 您似乎想忽略数字的指数。 One way to do that would be: 一种方法是:

int accel = Integer.parseInt(String.valueOf(randomAccel).replaceAll("-?(\\d+).*", "$1"));
 int accel = (int)Math.abs(randomAccel);

This line performs 3 steps 该行执行3个步骤

  1. Get the absolute value of randomAccel 获取randomAccel的绝对值
  2. Cast the value to an int (floating point numbers are truncated) 将值强制转换为int (浮点数将被截断)
  3. Assign the int value to accel 分配int值以accel

Now lets look what happens when the input is in the range [-9.4182062E-9,9.593071E- 8] (By the way -5.485747E-5 is not in this range.) 现在让我们看一下当输入在[-9.4182062E-9,9.593071E- 8]范围内时会发生什么(顺便说一下-5.485747E-5不在此范围内。)

Input value -9.4E-9 = -0.0000000094

1. -9.4E-9 converted to 9.4E-9
2. 9.4E-9 cast to int, fractional part is removed. Value is 0
3. 0 is assigned to `accel`

If you just want to most significant digit, you could multiply by 10^8 and see if that value is greater than 0 , if it is then trucate, else multiply by 10 then truncate: 如果只想获得最高有效位,则可以乘以10^8并查看该值是否大于0 ,如果该值是trucate,否则乘以10则截断:

int accel = 0;
r = Math.abs( randomAccel ) * 100000000;
if( r >= 10 ) {/* error out of range */}
else if( r > 0 ) accel = (int)r;
else
{
    r *= 10;
    accel = (int)r;
}
import static java.lang.Math.*;

Convert part: 转换部分:

double input =-5.485747E-5; //for example
double tmp = abs(input);
tmp *= pow(10, abs(floor(log10(tmp))));
int out = (int) tmp;
System.out.println(out);

output: 输出:

5

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

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