简体   繁体   中英

calculator , printing a decimal

im working on a calculator using android studio. Im having a really hard time trying to get my decimal button to work right. I have no idea how to get it to print a decimal to the screen. this is what i have below , which doesnt work.

private void deciDisplay(int j) {
    str = "." + Integer.toString(j);
    num = Integer.valueOf(str).intValue();
    disp.setText(str);

}

public void buttonDeci(View v) {

    deciDisplay(((int) num));

}

Like Golem said, you need to use a double or float, as Integers only have full numbers:

private void deciDisplay(int j) {
    str = "." + Integer.toString(j); //Or String.valueOf(j);
    num = Double.parseDouble(str);
    disp.setText(str);

}

Edit:

This may solve your actual problem of simply putting a dot to your TextView:

public void buttonDeci(View v){
    num+=".";
    disp.setText(num);
}

Assuming num is a String. If it's not, you somehow need an index to tell your calculator that you're behind the decimal dot now.

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