Decimal fraction to binary

  • Thread starter kadaj6
  • Start date
31
0
ok so i have this code:

Code:
import java.util.Scanner;

class doubleEncodingClass {

	public static void main(String args[]) {

		byte largestPositiveByte   = 127;
		short largestPositiveShort = 32767;
		int largestPositiveInt     = 2147483647;
		long largestPositiveLong   = 9223372036854775807L;
		long largestPositiveLongPlusOne   = 9223372036854775807L;

		Scanner in = new Scanner(System.in);
		
		System.out.println("Next number (0 to stop): ");
		double l1 = in.nextDouble();
		double nextNumber = Math.ceil(l1);	
		double l2= (nextNumber - l1);
		
		
		while (nextNumber != 0) {
			
			int radix;
			
			int numBits = 8;  // FIX: Closest power of two greater or equal to minimal number of bits required by next number
			
			System.out.println("Bits Required: " +  numBits);
			
			radix = 10;
			System.out.println("Decimal: " + String.format("%s",  Long.toString( l,radix)).replace(' ','0'));

			radix = 2;
			System.out.println("Binary: " + String.format("%"+numBits+"s",  Long.toString( l,radix)).replace(' ','0'));

			radix = 8;
			System.out.println("Octal: " + String.format("%"+((int) Math.ceil(numBits/3.0))+"s",  Long.toString( l,radix)).replace(' ','0'));

			radix = 16;
			System.out.println("Hexadecimal: 0x" + String.format("%"+numBits/4+"s",  Long.toString( l,radix)).replace(' ','0'));
			
			System.out.println("");
			System.out.println("Next number: (0 to stop)");
			nextNumber = in.nextDouble();
		}
		System.out.println("Good Bye");
	}
}
im trying to grab the decimal part of "l2" and represent it in binary,
this codes needs to tell me the binary of whole numbers and the decimal part of numbers...

plz help me
 

jim mcnamara

Mentor
3,435
1,626
Try an abstract datatype - use a class as "structs" with members having a public access specifier and no methods .
 

Want to reply to this thread?

"Decimal fraction to binary" You must log in or register to reply here.

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top