关于 Double类型如何判断是否相等

我是这样想的...
public class MainActivity extends AppCompatActivity {

    private double d1=0.11, d2=0.21;
    private static final double d = 0.0001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (d1 - d2 > -d && d1 - d2 < d){
            Toast.makeText(this,"d1与d2相等",Toast.LENGTH_LONG).show();
        }
              Toast.makeText(this,"d1与d2不相等",Toast.LENGTH_LONG).show();
    }
}

你可能感兴趣的:(java)