Separating double into integer and decimal parts

前端 未结 11 1057
日久生厌
日久生厌 2020-12-06 10:20

I am trying to separate a double into the integer and decimal parts

So for example, the number 24.4 should be separated into 24 and 4.

int integer =          


        
11条回答
  •  一整个雨季
    2020-12-06 10:50

    As RGO stated, you can do that and also round the decimal number in order to get the appropriate value. Ex:

    double number = 20.57;
    
    int integer = (int)number;
    double decimal = (10 * number - 10 * integer)/10;
    double temp =  Math.round(decimal*100.0)/100.0;
    System.out.println("Int = " + integer + "\nDecimal = " + temp);
    

提交回复
热议问题