# 把一个带小数的十进制数转化为R进制数输出，R可为二进制、八进制或十六进制，从键盘输入

## 一、读取控制台输入的数据

1.需要转化的带小数的数
2.目标转化进制
3.小数的精度
4.将这个数的整数部分与小数部分分离

		System.out.println("请输入一个数");
Scanner sc = new Scanner(System.in);
double num = sc.nextDouble();
System.out.println("请输入目标进制");
Scanner sc1 = new Scanner(System.in);
int num1 = sc1.nextInt();
System.out.println("请输入精度");
Scanner sc2 = new Scanner(System.in);
int acc = sc2.nextInt();

//整数
int int_num = (int) num;
//小数
double decimal1 =  num - int_num;



## 二、判断目标进制的值

1.二进制到九进制的整数
2.11进制到16进制的整数
3超出上面两类的整数

10进制16进制
00
11
22
33
44
55
66
77
88
99
10A
11B
12C
13D
14E
15F

		//9进制以下的处理
if (num1 >=2 && num1 <= 9){
HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
hashMap.put(0,"0");
hashMap.put(1,"1");
hashMap.put(2,"2");
hashMap.put(3,"3");
hashMap.put(4,"4");
hashMap.put(5,"5");
hashMap.put(6,"6");
hashMap.put(7,"7");
hashMap.put(8,"8");
hashMap.put(9,"9");

String str_i = zhenShu(int_num,num1,hashMap);
//            System.out.println(s);
String str_d = xiaoShu(decimal1,num1,acc,hashMap);
//整合整数小数
String int_b = str_i + "." + str_d;
System.out.println(num+"的"+num1+"进制的结果为："+int_b);

//10-16进制处理
}else if (num1>=11){
HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
hashMap.put(0,"0");
hashMap.put(1,"1");
hashMap.put(2,"2");
hashMap.put(3,"3");
hashMap.put(4,"4");
hashMap.put(5,"5");
hashMap.put(6,"6");
hashMap.put(7,"7");
hashMap.put(8,"8");
hashMap.put(9,"9");
hashMap.put(10,"A");
hashMap.put(11,"B");
hashMap.put(12,"C");
hashMap.put(13,"D");
hashMap.put(14,"E");
hashMap.put(15,"F");

String str_i = zhenShu(int_num,num1,hashMap);
//            System.out.println(s);
String str_d = xiaoShu(decimal1,num1,acc,hashMap);
//整合整数小数
String int_b = str_i + "." + str_d;
System.out.println(num+"的"+num1+"进制的结果为："+int_b);

}else  {
System.out.println("请输入2到9或11到16的整数");
}


## 三、整数部分的处理

//整数部分
public static String zhenShu(int int_num, int num1, HashMap map){
int mod = 0;
String str = "";
if(int_num<16){
return map.get(int_num).toString();
}
while (int_num/num1!=0){
mod = int_num%num1;
str += map.get(mod);
int_num = int_num/num1;
if(int_num/num1==0){
mod = int_num%num1;
str += map.get(mod);
}
}
String s = reverse(str);
return s;

}


## 四、小数部分的处理


public static String xiaoShu(double decimal1, int num1, int acc, HashMap map){
//小数部分
StringBuffer decimal = new StringBuffer();
//            System.out.println(decimal);

int[] arr = new int[acc];
double decimal2 = 0;
for (int i = 0; i < arr.length; i++) {
decimal2 = decimal1*num1;
decimal1 = Math.floor(decimal2);
arr[i] = (int) decimal1;
decimal1 = decimal2 - decimal1;
decimal.append(map.get(arr[i]));
}
return decimal.toString();
}



## 五、完整代码演示


/*
3、把一个带小数的十进制数转化为R进制数输出，R可为二进制、八进制或十六进制，从键盘输入。
*/

import java.util.HashMap;
import java.util.Scanner;

public class Demo3 {
public static void main(String[] args) {
System.out.println("请输入一个数");
Scanner sc = new Scanner(System.in);
double num = sc.nextDouble();
System.out.println("请输入您想转的进制");
Scanner sc1 = new Scanner(System.in);
int num1 = sc1.nextInt();
System.out.println("请输入精度");
Scanner sc2 = new Scanner(System.in);
int acc = sc2.nextInt();

//整数
int int_num = (int) num;
//小数
double decimal1 =  num - int_num;

//9进制以下的处理
if (num1 >=2 && num1 <= 9){
HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
hashMap.put(0,"0");
hashMap.put(1,"1");
hashMap.put(2,"2");
hashMap.put(3,"3");
hashMap.put(4,"4");
hashMap.put(5,"5");
hashMap.put(6,"6");
hashMap.put(7,"7");
hashMap.put(8,"8");
hashMap.put(9,"9");

String str_i = zhenShu(int_num,num1,hashMap);
//            System.out.println(s);
String str_d = xiaoShu(decimal1,num1,acc,hashMap);
//整合整数小数
String int_b = str_i + "." + str_d;
System.out.println(num+"的"+num1+"进制的结果为："+int_b);

//10-16进制处理
}else if (num1>=11){
HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
hashMap.put(0,"0");
hashMap.put(1,"1");
hashMap.put(2,"2");
hashMap.put(3,"3");
hashMap.put(4,"4");
hashMap.put(5,"5");
hashMap.put(6,"6");
hashMap.put(7,"7");
hashMap.put(8,"8");
hashMap.put(9,"9");
hashMap.put(10,"A");
hashMap.put(11,"B");
hashMap.put(12,"C");
hashMap.put(13,"D");
hashMap.put(14,"E");
hashMap.put(15,"F");

String str_i = zhenShu(int_num,num1,hashMap);
//            System.out.println(s);
String str_d = xiaoShu(decimal1,num1,acc,hashMap);
//整合整数小数
String int_b = str_i + "." + str_d;
System.out.println(num+"的"+num1+"进制的结果为："+int_b);

}else  {
System.out.println("请输入2到9或11到16的整数");
}
}
//顺序转换
public static String reverse(String str){
StringBuffer sb = new StringBuffer(str);
StringBuffer reverse = sb.reverse();
String s = reverse.toString();
return s;
}

//整数部分
public static String zhenShu(int int_num, int num1, HashMap map){
//整数部分
int mod = 0;
String str = "";
if(int_num<16){
return map.get(int_num).toString();
}
while (int_num/num1!=0){
mod = int_num%num1;
str += map.get(mod);
int_num = int_num/num1;
if(int_num/num1==0){
mod = int_num%num1;
str += map.get(mod);
}
}
String s = reverse(str);
return s;

}

public static String xiaoShu(double decimal1, int num1, int acc, HashMap map){
//小数部分
StringBuffer decimal = new StringBuffer();
//            System.out.println(decimal);

int[] arr = new int[acc];
double decimal2 = 0;
for (int i = 0; i < arr.length; i++) {
decimal2 = decimal1*num1;
decimal1 = Math.floor(decimal2);
arr[i] = (int) decimal1;
decimal1 = decimal2 - decimal1;
decimal.append(map.get(arr[i]));
}
return decimal.toString();
}

}


05-11
04-12 1万+
06-09 5575
05-06 4819
07-18 324
03-05 1万+
05-30 334
10-03 6万+
01-04 1万+
06-05 9815
12-31
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林