二进制小数转换成Java中十进制

编程入门 行业动态 更新时间:2024-10-18 20:27:27
本文介绍了二进制小数转换成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中十进制

本文发布于:2023-10-24 20:04:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524872.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小数   转换成   中十进制   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!