Java从科学记数法转换为浮点型
我正在开发一个程序,可以有效地将二进制、十进制或十六进制数字转换为其他格式(不要问我为什么这样做,老实说我不知道)。到目前为止,我只进行了二进制 - 十进制转换,并且工作正常,但是每当输入的二进制数是 8 位或更多时,它就会崩溃。
据我所知,当我输入数字 10011001 时,它会被转换为科学记数法并变成 1.0011001E7,这实际上并不是一个问题,除了我转换数字的方式涉及创建一个与数字并将其分解为单个字符。不幸的是,这意味着我有一个值为“1.0011001E7”而不是“10011001”的字符串,所以当我剪切字符时,我点击了“。”当我尝试用它进行计算时,程序不知道该怎么做。所以基本上我的问题归结为这个,我如何强制它使用非科学记数法版本进行这些计算?
感谢您的所有帮助,如果有帮助的话,这里是代码:
//This Splits A Single String Of Digits Into An Array Of Individual Digits
public float[] splitDigits(float fltInput){
//This Declares The Variables
String strInput = "" + fltInput;
float[] digit = new float[strInput.length() - 2];
int m = 0;
//This Declares The Array To Hold The Answer
for (m = 0; m < (strInput.length() - 2); m++){
digit[m] = Float.parseFloat(strInput.substring(m, m + 1)); //Breaks here
}
//This Returns The Answer
return digit;
}
I have a program that I am working on which would effectively convert binary, decimal, or hex numbers to other formats (don't ask why I'm doing this, I honestly don't know). So far I only have a binary - decimal conversion going, and it works fine however whenever the binary number entered is 8 digits or more it crashes.
As far as I can tell when I input the number 10011001 it gets translated to scientific notation and becomes 1.0011001E7 which wouldn't really be a problem, except that the way I am converting the numbers involves creating a string with the same value as the number and breaking it into individual characters. Unfortunately, this means I have a string valued "1.0011001E7" instead of "10011001", so when I cut up the characters I hit the "." and the program doesn't know what to do when I try to make calculations with that. So basically my question comes down to this, how do I force it to use the not-scientific notation version for these calculations?
Thanks for all your help, and here is the code if it helps at all:
//This Splits A Single String Of Digits Into An Array Of Individual Digits
public float[] splitDigits(float fltInput){
//This Declares The Variables
String strInput = "" + fltInput;
float[] digit = new float[strInput.length() - 2];
int m = 0;
//This Declares The Array To Hold The Answer
for (m = 0; m < (strInput.length() - 2); m++){
digit[m] = Float.parseFloat(strInput.substring(m, m + 1)); //Breaks here
}
//This Returns The Answer
return digit;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用 BigDecimal
注意这里的 fltInput将自动转换为双精度。
Just use BigDecimal
Note here the fltInput will be automatically converted to a double.