编写一个Java程序,将十进制数(double型)转换为二进制数的字符串
答案:1 悬赏:70 手机版
解决时间 2021-03-19 08:46
- 提问者网友:不爱我么
- 2021-03-19 03:33
编写一个Java程序,将十进制数(double型)转换为二进制数的字符串
最佳答案
- 五星知识达人网友:你哪知我潦倒为你
- 2021-03-19 04:46
double型转二进制字符串涉及到小数部分,没法像Integer那样直接转,底下是一个转换类
public class Test {
public static void main(String[] args) {
Test t = new Test();
double d = 8;
String s = t.decimal2BinaryStr(d);
System.out.println("十进制数"+d+"转成二进制数为:"+s);
}
public String decimal2BinaryStr(double d){
String result = decimal2BinaryStr_Inte(d);
result += decimal2BinaryStr_Deci(d);
return result;
}
public String decimal2BinaryStr_Inte(double d){
// return Integer.toBinaryString((int)d);
String result = "";
long inte = (long)d;
int index = 0;
while(true){
result += inte%2;
inte = inte/2;
index++;
if(index%4 == 0){
result+=" ";
}
if(inte==0){
while(index%4!=0){
result+="0";
index++;
}
break;
}
}
char[] c = result.toCharArray();
char[] cc = new char[c.length];
for(int i=c.length; i>0; i--){
cc[cc.length-i] = c[i-1];
}
return new String(cc);
}
public String decimal2BinaryStr_Deci(double d){
return decimal2BinaryStr_Deci(d, 0);
}
public String decimal2BinaryStr_Deci(double d, int scale){
double deci = sub(d,(long)d);
if(deci==0){
return "";
}
//为了防止程序因所转换的数据转换后的结果是一个无限循环的二进制小数,因此给其一个默认的精确度
if(scale==0){
scale = (String.valueOf(deci).length()-2)*4;
}
int index = 0;
StringBuilder inteStr = new StringBuilder();
double tempD = 0.d;
while(true){
if(deci==0 || index==scale){
while(index%4!=0){
inteStr.append("0");
index++;
}
break;
}
if(index==0){
inteStr.append(".");
}
tempD = deci*2;
inteStr.append((int)tempD);
deci = sub(tempD ,(int)tempD);
index++;
if(index%4 == 0){
inteStr.append(" ");
}
}
return inteStr.toString();
}
public static double sub(double v1, double v2) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.subtract(b2).doubleValue();
}
}
public class Test {
public static void main(String[] args) {
Test t = new Test();
double d = 8;
String s = t.decimal2BinaryStr(d);
System.out.println("十进制数"+d+"转成二进制数为:"+s);
}
public String decimal2BinaryStr(double d){
String result = decimal2BinaryStr_Inte(d);
result += decimal2BinaryStr_Deci(d);
return result;
}
public String decimal2BinaryStr_Inte(double d){
// return Integer.toBinaryString((int)d);
String result = "";
long inte = (long)d;
int index = 0;
while(true){
result += inte%2;
inte = inte/2;
index++;
if(index%4 == 0){
result+=" ";
}
if(inte==0){
while(index%4!=0){
result+="0";
index++;
}
break;
}
}
char[] c = result.toCharArray();
char[] cc = new char[c.length];
for(int i=c.length; i>0; i--){
cc[cc.length-i] = c[i-1];
}
return new String(cc);
}
public String decimal2BinaryStr_Deci(double d){
return decimal2BinaryStr_Deci(d, 0);
}
public String decimal2BinaryStr_Deci(double d, int scale){
double deci = sub(d,(long)d);
if(deci==0){
return "";
}
//为了防止程序因所转换的数据转换后的结果是一个无限循环的二进制小数,因此给其一个默认的精确度
if(scale==0){
scale = (String.valueOf(deci).length()-2)*4;
}
int index = 0;
StringBuilder inteStr = new StringBuilder();
double tempD = 0.d;
while(true){
if(deci==0 || index==scale){
while(index%4!=0){
inteStr.append("0");
index++;
}
break;
}
if(index==0){
inteStr.append(".");
}
tempD = deci*2;
inteStr.append((int)tempD);
deci = sub(tempD ,(int)tempD);
index++;
if(index%4 == 0){
inteStr.append(" ");
}
}
return inteStr.toString();
}
public static double sub(double v1, double v2) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.subtract(b2).doubleValue();
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯