How to get the decimal part of a number in Java

2 Answers

0 votes
public class MyClass {
    public static void main(String args[]) {
        double d = 3.1415;

        System.out.println(d - (int)d);
    }
}




/*
run:

0.14150000000000018

*/

 



answered Jun 14, 2022 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        double d = 3.1415;
        
        String s = String.valueOf(d);
        
        int index = s.indexOf(".");

        System.out.println(s.substring(index));
        System.out.println(s.substring(index + 1));
    }
}




/*
run:

.1415
1415

*/

 



answered Jun 14, 2022 by avibootz

Related questions

4 answers 247 views
2 answers 198 views
2 answers 200 views
1 answer 160 views
1 answer 114 views
1 answer 138 views
...