Insights Blog
-- Browse All Articles --
Physics Articles
Physics Tutorials
Physics Guides
Physics FAQ
Math Articles
Math Tutorials
Math Guides
Math FAQ
Education Articles
Education Guides
Bio/Chem Articles
Technology Guides
Computer Science Tutorials
Forums
Chemistry
Biology and Medical
Earth Sciences
Computer Science
Computing and Technology
DIY Projects
Trending
Featured Threads
Log in
Register
What's new
Search
Search
Search titles only
By:
Chemistry
Biology and Medical
Earth Sciences
Computer Science
Computing and Technology
DIY Projects
Menu
Log in
Register
Navigation
More options
Contact us
Close Menu
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Forums
Other Sciences
Programming and Computer Science
What is a way to convert a decimal fraction to binary using a custom data type?
Reply to thread
Message
[QUOTE="kadaj6, post: 4512401, member: 441996"] 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"); } }[/CODE] 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 [/QUOTE]
Insert quotes…
Post reply
Forums
Other Sciences
Programming and Computer Science
What is a way to convert a decimal fraction to binary using a custom data type?
Back
Top