Hi, I was wondering if there is a way to split double into its integer part and decimal part.
I' working on binary converter, and my teacher gave me 4 steps on how to write the program.
I've completed step 1 which is separating number into integer and decimal portion, and step 2 which is converting integer portion. However, in step 3 I have to multiply decimal part by 2, and then split it into integer and decimal portion. It was easy in step 1 because the number was a string from a keyboard, but in step 3 the decimal part is double so I can't use the substring or split method for substring. :( Can you guys please help me?
This is my code for up to step 3:
(I'm using ArrayStack and ArrayQueue classes that I wrote).

public static void main(String[] args) 
    {

        String answer;  
        Scanner keybrd = new Scanner(System.in);    
        ArrayStack<Integer> intStack = new ArrayStack <Integer>();
        ArrayQueue<Double> decQueue = new ArrayQueue<Double>();
        do
        {

            /*
             * Step 1 - separating the base 10 number
             */

            String number="";   
            int intPart=0;
            double decPart=0;
            String decimalPart= "";
            String integerPart = "";

            System.out.println("Enter a number to be converted:");
            number = keybrd.nextLine(); 

            for(int i = 0; i < number.length(); i++)
            {
                if (number.charAt(i) == '.')
                {       
                    while (i < number.length())
                    {                       
                        decimalPart +=number.charAt(i);
                        i++;
                    }
                    decPart = Double.parseDouble(decimalPart);
                }
                else
                {
                    integerPart +=number.charAt(i);
                    intPart = Integer.parseInt(integerPart);
                }
            }

            /*
             * Step 2 - intPart
             */
            do 
            {
                intStack.push(intPart%2);
                intPart = intPart/2;        
            }
            while (intPart !=0);    

            /*
             * Step 3 - decPart
             */
            Double decPartMult = decPart *2;

Recommended Answers

All 2 Replies

double d = 32.456;
System.out.println( (int)d );
System.out.println( d-(int)d );
commented: Somebody please give him something complex, he is getting borred... ;) +7

Thank You!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.