NumSystemsConverter.java 916B

123456789101112131415161718192021222324252627282930
  1. public class NumSystemsConverter
  2. {
  3. // display mode is sent along with displayNumber, could also be defined globally)
  4. public String updateDisplayNumber(double displayNumber,int mode){
  5. String str="";
  6. switch(mode) {
  7. case 1:{
  8. str=Double.toString(displayNumber);
  9. break;
  10. }
  11. case 2:{
  12. str=Long.toBinaryString(Double.doubleToRawLongBits(displayNumber));
  13. break;
  14. }
  15. case 3:{
  16. str=Double.toHexString(displayNumber);
  17. break;
  18. }
  19. case 4:{
  20. double fraction = (displayNumber%1);
  21. double integral = (displayNumber-fraction);
  22. str=Integer.toOctalString((int)integral)+"."+Integer.toOctalString((int)(fraction+.5));
  23. break;
  24. }
  25. }
  26. return str;
  27. }
  28. }