26进制转10进制java_二进制转化为十进制Java实现

二进制转化为十进制

①按权展开方法Java实现

/*

* 按权展开法

*/

public static double BinToTen1(String binary) {

//查找该二进制是否存在小数点

int index = binary.indexOf('.');

//转化成的十进制

double ten = 0;

//整数部分

double integer = 0;

//小数部分

double decimal = 0;

//index等于-1,说明没有小数部分

if(index == -1) {

for(int i = 0; i < binary.length(); i++) {

//48为字符'0'对应的ASCII值;

//ten += (binary.charAt(i) - 48) * Math.pow(2, -(i-(binary.length() - 1)));

//或者可以将字符转化为字符串,再由字符串转化为数字

ten += Integer.parseInt(String.valueOf(binary.charAt(i))) * Math.pow(2, -(i-(binary.length() - 1)));

}

} else {

//计算整数部分

for(int i = 0; i < index; i++) {

integer += Integer.parseInt(String.valueOf(binary.charAt(i))) * Math.pow(2, -(i-(index - 1)));

}

//计算小数部分

for(int j = index + 1; j < binary.length(); j++) {

decimal += Integer.parseInt(String.valueOf(binary.charAt(j))) * Math.pow(2, (index - j));

}

ten = integer + decimal;

}

return ten;

}

测试

public static void main(String[] args) {

double a = BinToTen1("11");

System.out.println(a);

double b = BinToTen1("0.11");

System.out.println(b);

double c = BinToTen1("11.111");

System.out.println(c);

}

8ec19c84e5ca1b604726d82c4e108c1d.png

②基位连乘、连除法Java实现

/*

* 基位连乘、连除法

*/

public static double BinToTen2(String binary) {

//查找该二进制是否存在小数点

int index = binary.indexOf('.');

//转化成的十进制

double ten = 0;

//整数部分

double integer = 0;

//小数部分

double decimal = 0;

double temp = 0;

//index等于-1,说明没有小数部分

if(index == -1) {

if(binary.length() == 0) {

return ten;

}else if(binary.length() == 1) {

ten = Integer.parseInt(String.valueOf(binary.charAt(0)));

return ten;

} else {

ten = Integer.parseInt(String.valueOf(binary.charAt(0)));

for(int i = 1; i < binary.length(); i++) {

//最高位乘以2加下一位,直到加到最低为位置

ten = ten * 2 + Integer.parseInt(String.valueOf(binary.charAt(i)));

}

}

} else {

if(index > 0) {

//计算整数部分

integer = Integer.parseInt(String.valueOf(binary.charAt(0)));

for(int i = 1; i < index; i++) {

//最高位乘以2加下一位,直到加到最低为位置

integer = integer * 2 + Integer.parseInt(String.valueOf(binary.charAt(i)));

}

}

if(index != binary.length() - 1) {

decimal = Integer.parseInt(String.valueOf(binary.charAt(binary.length() - 1)));

//计算小数部分

for(int j = binary.length() - 2; j > index; j--) {

decimal = decimal/2 + Integer.parseInt(String.valueOf(binary.charAt(j)));

}

}

ten = integer + decimal/2;

}

return ten;

}

测试

public static void main(String[] args) {

double a = BinToTen2("1.011");

System.out.println(a);

double b = BinToTen2("0.011");

System.out.println(b);

double c = BinToTen2(".011");

System.out.println(c);

double d = BinToTen2("11.");

System.out.println(d);

}

3f3f34fc1abffeff982b0046ce0c426a.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值