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

In summary, the conversation is about using an abstract datatype, specifically a class with public members and no methods, to represent the decimal part of a number in binary. The goal is to have a program that can display both the binary representation of whole numbers and the decimal part of numbers.
  • #1
kadaj6
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...

please help me
 
Technology news on Phys.org
  • #2
Try an abstract datatype - use a class as "structs" with members having a public access specifier and no methods .
 

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

What is decimal fraction to binary conversion?

Decimal fraction to binary conversion is the process of converting a decimal number with a fractional part to its binary equivalent. This is done by representing the fractional part as a sum of powers of 2.

Why is decimal fraction to binary conversion important?

Decimal fraction to binary conversion is important because binary is the fundamental language of computers. By converting decimal fractions to binary, we can accurately represent and manipulate these values in computer systems.

What is the process for converting a decimal fraction to binary?

The process for converting a decimal fraction to binary involves repeatedly multiplying the fractional part by 2 and recording the resulting whole number and fractional part. This process is continued until the fractional part becomes 0 or the desired accuracy is achieved. The resulting binary numbers are then combined to form the binary equivalent of the decimal fraction.

What are some tips for converting a decimal fraction to binary accurately?

Some tips for converting a decimal fraction to binary accurately include understanding the concept of place values, rounding the final binary number to the desired accuracy, and double-checking the conversion using a calculator or an online converter.

Are there any shortcuts or tricks for converting decimal fractions to binary?

Yes, there are some shortcuts and tricks for converting decimal fractions to binary. One example is using the binary fraction equivalent of common decimal fractions, such as 0.5, 0.25, and 0.125. Another trick is using the concept of binary complements, where the binary equivalent of a decimal fraction is found by subtracting the binary equivalent of the whole number from 1.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
18K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top