java Android - 1 除以 2 = 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10555690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:32:44  来源:igfitidea点击:

Android - 1 divided by 2 = 0

javaandroid

提问by TechAUmNu

I have been trying to use my new IOIO for android, and needed to find a frequency of a signal.
So I converted the signal to binary and then did 1 divided by the time between 1's. Although when I did this I found that I got 0 as my output. I then decided to see what 1 / 2 gave me, and to my surprise it also gave 0! Anyone have any idea why this is the case?

我一直在尝试将我的新 IOIO 用于 android,并且需要找到信号的频率。
所以我将信号转换为二进制,然后将 1 除以 1 之间的时间。虽然当我这样做时,我发现我的输出为 0。然后我决定看看 1 / 2 给了我什么,令我惊讶的是它也给了 0!任何人都知道为什么会这样?

Code: private float frequency = 1/2;

代码: private float frequency = 1/2;

Could this be todo with using Float.toString(frequency)?

这可能与使用有关Float.toString(frequency)吗?

回答by Oleksi

This is an example of integer division. Try:

这是整数除法的一个例子。尝试:

private float frequency = 1.0/2.0;

Java will execute 1/2, which yields 0.5. However, since Java sees this as operations on integers (and 0.5 isn't an integer), it'll truncate the decimal and leave just the integer part, 0. By telling Java to work with floats (1.0 vs. 1), you tell it to keep the decimal part of the intermediate computation.

Java 将执行 1/2,结果为 0.5。但是,由于 Java 将其视为对整数的运算(并且 0.5 不是整数),因此它会截断小数并只保留整数部分 0。通过告诉 Java 使用浮点数(1.0 对 1),您告诉它保留中间计算的小数部分。

回答by Affe

1 and 2 are integers in java. If you want them to be explicitly interpreted as floats you need to do.

1和2是java中的整数。如果您希望它们被明确解释为浮点数,您需要这样做。

private float frequency = 1f/2f;

If you don't care if the math is actually done on a double first, then just this is easier to read:

如果您不关心数学是否实际上是先在 double 上完成的,那么这更容易阅读:

private float frequency = 1.0/2.0;

回答by Fahim Parkar

You need to cast. Here is more information

你需要投射. 这里有更多信息

float myValue = (float) 1/2;

float myValue = (float) 1/2;

class Main {
    public static void main(String[] args) {
        float myValue = (float) 1/2;
        System.out.println("1/2 = " + myValue);
    }
}

Output would be 1/2 = 0.5

输出将是 1/2 = 0.5

回答by MByD

This is integer division (only whole numbers).

这是整数除法(仅限整数)。

Try:

尝试:

private float frequency = 1/2.0;

回答by htafoya

the 1 and 2 are integers, and they return an integer.

1 和 2 是整数,它们返回一个整数。

Try creating two floats with those values before doing the division.

在进行除法之前尝试使用这些值创建两个浮点数。

回答by SQLMenace

When you do aritmetic with integers the resul becomes an integer also add .0

当你对整数进行算术运算时,结果变成一个整数也加上 .0

so

所以

private float frequency = 1.0/2.0;

回答by Chintan Raghwani

use

利用

    private float frequency = 1F/2F;

回答by JoB?N

If you are dividing two integers and you are not getting the correct output just caste the evaluated intresult to floatas shown below.

如果您将两个整数相除并且没有得到正确的输出,只需将评估int结果转换float为如下所示。

    int b = 2;
    int a = 1;

    float out = (float) a / b;
    System.out.println(out);

This will produce the output 0.5

这将产生输出 0.5