What is a way to convert a decimal fraction to binary using a custom data type?

  • Thread starter Thread starter kadaj6
  • Start date Start date
  • Tags Tags
    Binary Fraction
AI Thread Summary
The discussion revolves around a Java code snippet that aims to convert numbers input by the user into various numeral systems, including decimal, binary, octal, and hexadecimal. The user is specifically seeking assistance in extracting the decimal part of a double variable, referred to as "l2," and representing it in binary format. The code currently calculates the next whole number using `Math.ceil()` and prints the required bits for representation but does not address the conversion of the decimal portion. The user is advised to consider using an abstract data type, suggesting the implementation of a class with public members to facilitate this functionality without methods. The focus remains on enhancing the code to achieve the desired output for both whole and decimal numbers.
kadaj6
Messages
31
Reaction score
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...

please help me
 
Technology news on Phys.org
Try an abstract datatype - use a class as "structs" with members having a public access specifier and no methods .
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top