Search code examples
javaintegerdouble

java how to delete digits after comma in double data type


so for example what I have and what I want it to, first numbers are double and I would like them to be integer but with no zeros


2.00 -> 2


5.012 -> 5


I tried with this, but still doesn't work

if(result % 1 == 0){
    String temp = String.valueOf(result);
    int tempInt = Integer.parseInt(temp);
    tekst.setText(String.valueOf(tempInt));

    }else {
     tekst.setText(String.valueOf(result));
     num1 = 0;
     num2 = 0;
     result = 0;
}

result variable is double, also this shows after compiling

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "887.0"

Solution

  • This is what i would do.

    double num1 = 2.3;
    double num2 = 4.5;
    

    Later...

        int num11 = (int) num1;
        int num22 = (int) num2;
        System.out.println(num11 + ", " + num22 + ".");
    

    The result would be:

    2, 4.

    That's all.