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
Click For Summary
SUMMARY

The discussion focuses on converting decimal fractions to binary using a custom data type in Java. The provided code snippet demonstrates how to read a decimal number, calculate its binary representation, and display it alongside its decimal, octal, and hexadecimal forms. The user seeks assistance in modifying the code to accurately represent the binary equivalent of the decimal part of a number. A suggestion is made to utilize an abstract data type, specifically a class with public member variables and no methods, to achieve this functionality.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of data types in Java, particularly double and long
  • Knowledge of number systems: binary, octal, decimal, hexadecimal
  • Familiarity with Java's Scanner class for input handling
NEXT STEPS
  • Implement a custom class in Java to represent decimal fractions as binary
  • Research Java's BigDecimal class for precise decimal representation
  • Learn about binary conversion algorithms for fractional numbers
  • Explore Java's String.format method for formatting output
USEFUL FOR

Java developers, computer science students, and anyone interested in numerical representation and conversion techniques in programming.

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 .
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
Replies
3
Views
3K
Replies
4
Views
3K
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
2
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
22K
  • · Replies 2 ·
Replies
2
Views
18K
  • · Replies 1 ·
Replies
1
Views
2K