import static java.lang.Math.floor;
import static java.lang.Math.pow;

public class DecimalBinary {

    private String vorkomma;
    private String nachkomma;

    private String number;

    public DecimalBinary(int vorkomma, double nachkomma) {
        this.vorkomma = convertVorkomma(vorkomma, 2);
        this.nachkomma = convertNachkomma(nachkomma);
    }

    public DecimalBinary(String number, int baseA, int baseB) {
        long val = getDecimal(number, baseA);
        this.number =  convertVorkomma(val, baseB);
        System.out.println(number + "(" + baseA + ") = " + this.number + "(" + baseB + ")");
    }

    private String convertVorkomma(long num, int sys) {
        if(num == 0)
            return "0";
        //get highest potenz, die gerade noch reinpasst
        int o = 0;
        while(pow(sys, o) <= num){
            o++;
        }
        o--;
        //checke wie oft die Zahl reinpasst und subtrahiere das dann
        String result = "";
        for(int h = o; h >= 0; h--){
            long pot = (long) pow(sys, h);
            int wieoft = (int) floor(num/pot);
            num -= wieoft*pot;
            result += valueToChar(wieoft);
        }
        return result;
    }

    private char valueToChar(int s) {
        if (s < 10)
            return (char) ('0' + s);
        else
            return (char) ('A' + s - 10);
    }

    private int charToValue(char s){
        if(s - '0' >= 0 && '9' - s >= 0)
            return s -'0';
        else
            return 10 + s - 'A';
    }

    private long getDecimal(String num, int sys) {
        long result = 0;
            for(int i =  0; i < num.length(); i++){
                result += charToValue(num.charAt(i)) * pow(sys, num.length()-i-1);
            }
            return result;
    }

    private String convertNachkomma(double nach) {
        String nachkomma = "";
        double result = nach;
        do {
            //System.out.print(result + "* 2 = ");
            result *= 2;
            //System.out.print(result);
            if(result >= 1) {
                result -= 1;
                nachkomma += "1";
                //System.out.println("  --> " + "1");
            } else {
                nachkomma += "0";
                //System.out.println("  --> " + "0");
            }
        } while (result != 0);
        return nachkomma;
    }

    public void printNumber() {
        System.out.println(vorkomma + "," + nachkomma);
    }

    public static void main(String args[]) {
        DecimalBinary db = new DecimalBinary(57, 0.40625);
        db.printNumber();
        db = new DecimalBinary("2120", 3, 2);
        db = new DecimalBinary("110", 10, 2);
        db = new DecimalBinary("133", 10, 2);
        db = new DecimalBinary(43, 0.125);
        db.printNumber();
    }
}
