Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,872 questions

51,796 answers

573 users

How to extract the int part and the decimal part from double number in Java

3 Answers

0 votes
public class MyClass {

    public static void main(String args[]) {
        double d = 234.872;
        int int_part = (int)d;
        double fraction_part = d - int_part;
        
        System.out.println(int_part);
        System.out.println(fraction_part);
    }
}



/*
run:

234
0.8720000000000141

*/

 



answered Aug 28, 2019 by avibootz
0 votes
public class MyClass {
    
    public static void main(String args[]) {
        double d = 234.872;
        String s = String.valueOf(d);
        int index_of_decimal_point = s.indexOf(".");

        System.out.println(s.substring(0, index_of_decimal_point));
        System.out.println(s.substring(index_of_decimal_point));
        System.out.println(s.substring(index_of_decimal_point + 1));
    }
}



/*
run:

234
.872
872

*/

 



answered Aug 28, 2019 by avibootz
0 votes
import java.math.BigDecimal; 

public class MyClass {
    
    public static void main(String args[]) {
        double d = 234.872;
        BigDecimal big_decimal = new BigDecimal(String.valueOf(d));
        int int_part = big_decimal.intValue();
        String fraction_part = big_decimal.subtract(new BigDecimal(int_part)).toPlainString();

        System.out.println(int_part);
        System.out.println(fraction_part);
    }
}



/*
run:

234
0.872

*/

 



answered Aug 28, 2019 by avibootz
...