Separating double into integer and decimal parts

前端 未结 11 1055
日久生厌
日久生厌 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条回答
  •  -上瘾入骨i
    2020-12-06 10:46

    I figured out the way with little workaround and I am sure it works for all the cases.

    The code says it all.

            double number = input.nextDouble(); // 234.025
            String s = Double.toString(number); // "234.025"
            s = "0" + s.substring(s.indexOf(".")); //"0.025"
            double decimal = Double.parseDouble(s);// 0.025
            int num = (int)number; // 234
            System.out.println(decimal + " " + num); //0.025 234
    

提交回复
热议问题