public class BinarySystem { /** * Converts to a binary format a number written in a decimal format * @param decimal the String representing the number written in decimal format * @param digits the number of digits after the binary point in the binary representation * @return the String representing the number in binary format * @throws NumberFormatException if decimal is null or doesn't represent a number * or is a negative number, or its integer and fractional parts can't be written with longs */ public String toBinary(String decimal, int digits) { if (decimal == null) { throw new NumberFormatException(); } // Extract the integer and fractional part of the number decimal = decimal.trim(); int decimalPosition = decimal.indexOf('.'); if (decimalPosition == -1) { decimalPosition = decimal.length(); } String integerPart = ""; for (int i = 0; i < decimalPosition; i++) { char c = decimal.charAt(i); if (Character.isDigit(c)) { integerPart += c; } else { throw new NumberFormatException(); } } // Remove the leading zeros in the integer part boolean foundNonZero = false; for (int i = 0; i < integerPart.length() && !foundNonZero; i++) { if (integerPart.charAt(i) == '0') { integerPart = integerPart.substring(i+1); i --; // since the current character has been removed } else { foundNonZero = true; } } String fractionalPart = ""; for (int i = decimalPosition + 1 ; i < decimal.length(); i++) { char c = decimal.charAt(i); if (Character.isDigit(c)) { fractionalPart += c; } else { throw new NumberFormatException(); } } // Remove the trailing zeros in the fractional part foundNonZero = false; for (int i = fractionalPart.length() - 1; i >= 0 && !foundNonZero; i--) { if (fractionalPart.charAt(i) == '0') { fractionalPart = fractionalPart.substring(0, i); } else { foundNonZero = true; } } // Convert the integer part to binary // algorithm: Divide by increasing powers of 2. The remainder is the binary digit of // the binary representation for that power of 2. // example: 9 // 9 % 2 is 1 (digit for 2^0), 9 / 2 is 4 // 4 % 2 is 0 (digit for 2^1), 4 / 2 is 2 // 2 % 2 is 0 (digit for 2^2), 2 / 2 is 1 // 1 % 2 is 1 (digit for 2^3), 1 / 2 is 0 // thus 9 is written 1001 long intPart = 0; String binary = ""; if (integerPart.length() > 0) { intPart = Long.parseLong(integerPart); } do { binary = intPart % 2 + binary; intPart /= 2; } while (intPart > 0); // Convert the fractional part to binary // algorithm: multiply by increasing powers of 2. The carry is the binary digit of // the binary representation for that power of 2 // 3 (from 0.3) // 3 * 2 is 6. Carry = 0 (2^-1 digit) // 6 * 2 is 12. Carry = 1 (2^-2 digit) // 2 * 2 is 4. Carry = 0 (2^-3 digit) // 4 * 2 is 8. Carry = 0 (2^-4 digit) // 8 * 2 is 16. Carry = 1 (2^-5 digit) // 6 * 2 is 12. Carry = 1 (2^-6 digit) // 2 * 2 is 4. Carry = 0 (2^-7 digit) // etc ... // thus 0.3 is written 0.0100110... if (digits > 0 && fractionalPart.length() == 0) { // add a fractional part if none was provided // and the binary representation is required // to have digits after the binary point fractionalPart = "0"; } if (fractionalPart.length() > 0) { int n = fractionalPart.length(); // number of digits after the decimal point long fracPart = Long.parseLong(fractionalPart); binary += "."; long temp = fracPart; int count = 0; do { temp *= 2; count ++; if (numberOfDigits(temp) > n) { // carry? binary += "1"; temp -= (long) Math.pow(10, n); } else { binary += "0"; } } while(count < digits); } return binary; } /** * Returns the number of digits in the decimal representation of a given long * @param n the long whose digits are counted * @return the number of digits in n * Precondition: n is a non negative integer */ private int numberOfDigits (long n) { int count = 1; while (n >= 10) { count ++; n /= 10; } return count; } public static void main(String[] args) { BinarySystem s = new BinarySystem(); System.out.println(s.toBinary("2.3", 100)); } }