Converting a double from one number system to another
I wrote this code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int radix = scanner.nextInt();
String number = scanner.next();
int radixNew = scanner.nextInt();
if (radix == radixNew)
System.out.println(number);
else if (radix == 1)
System.out.println(Long.toString((long) Math.ceil(Math.log10(Long.parseLong(number))), radixNew));
else if (radixNew == 1) {
for (int i=1; i <= Long.parseLong(number, radix);i++)
System.out.print(1);
}
else
System.out.println(Long.toString(Long.parseLong(number, radix), radixNew));
}
}
How can I change it to work with double?(Double.toString and parseDouble only work with one parameter and do not allow you to convert numbers to other number systems)
0
1 answers
Double is encoded according to the IEEE 754 standard, which separates the mantissa, order, and sign (64 bits).
There is a method Double.doubleToRawLongBits(double value)
, which translates the bits of IEEE 754 into bits long
, then these bits long
can be translated into another number system.
2
Author: Barmaley, 2020-04-03 11:25:46