二进制小数转换成Java中十进制 [英] conversion of binary fraction to decimal in java

查看:719
本文介绍了二进制小数转换成Java中十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要二进制小数转换成decimal.Consider二进制数1101.1110。我知道转换的手工方法,我可以把程序的形式,但我不能够小数部分从数字分开。我需要,因为转换的方法中,后点的不同来分离它。

I want to convert a binary fraction into decimal.Consider binary number 1101.1110. I know the manual method of conversion and i can put in the form of program, but i am not able to separate the fractional part from the number. I need to separate it because the method of conversion differs after the point.

而十进制转换为二进制小数部分可以分开是这样的:

while converting decimal to binary the fractional part can be separated this way:

double num=12.23;

int fraction=num-(long)num;

u能帮助我。

先谢谢了。

推荐答案

我假设你想要的类型的结果双击。解析您的字符串不点,再由两对数字后点的数量的权利功率划分。

I'm assuming you want a result of type double. Parse your String without the point, then divide by the right power of two for the number of digits after the point.

int placesAfterPoint = binaryString.length() - binaryString.indexOf(".") - 1;
long numerator = Long.parseLong(binaryString.replace(".", ""), 2);
double value = ((double) numerator) / ( 1L << placesAfterPoint );

这篇关于二进制小数转换成Java中十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆