I want to convert a source base number to a destination base number, but I have a problem with fractions. When I trying to convert 10.234
(base10) to base 7 = 13.14315
it works perfectly, or aaaaa.0
(base16) to base 24 = 22df2
it also works.
But when I try to convert aaaaa.cdefb0
(base16) to base 24 = 22df2.j78da
it doesn't work. I can't calculate fraction part and I get 22df2
as the answer
my code for base conversion :
private static String baseConversion(String number,
int sBase, int dBase) {
return Long.toString(
Long.parseLong(number, sBase),
dBase);
}
my code for fraction conversion :
private static String fractionConversion(double fraction, int dBase) {
StringBuilder output = new StringBuilder(".");
for (int i = 0; i < PRECISION; i++) {
fraction *= dBase;
output.append(Long.parseLong(Integer.toString((int) fraction)));
fraction -= Long.parseLong(Integer.toString((int) fraction));
}
return output.toString();
}