简体   繁体   English

无法从String强制转换为Java中的float

[英]Cannot cast from String to float in java

long l = ((File)localObject).length();
  if (l >= 1024L)
  {
    if (l >= 1048576L)
    {
      if (l >= 1073741824L)
        str = Float.toString(Math.round(100.0F * ((float)l / 1.073742E+09F)) / 100.0F) + " GB";
      else
        str = Float.toString(Math.round(100.0F * ((float)str / 1048576.0F)) / 100.0F) + " MB";
    }
    else
      str = Float.toString(Math.round(100.0F * ((float)str / 1024.0F)) / 100.0F) + " KB";
  }
  else
    str = Long.toString(str) + " B";

else str = Long.toOctalString(str) + " B"; else str = Long.toOctalString(str)+“ B”; } return (String)str; } return(String)str; } }

Another error in 16 th Here i got the error in 9 th line and 12 th line Cannot cast from String to float in java 第16行中的另一个错误在这里我在第9行和第12行中出现错误无法从String强制转换为Java中的float

I suppose str is a String, in which case you need to use Float.valueOf(str) instead of (float)str . 我想str是一个字符串,在这种情况下,您需要使用Float.valueOf(str)而不是(float)str

Or more likely you meant (float)l instead of (float)str . 或者更可能是您意思是(float)l而不是(float)str

And as mentioned by enzom83, your last line should probably be: 就像enzom83提到的那样,您的最后一行可能应该是:

str = Long.toString(l) + " B";

You can't use (float)str when str is a String . strString时,不能使用(float)str

Replace it with this (and catch possible exceptions!): Float.valueOf(str) 替换为此(并捕获可能的异常!): Float.valueOf(str)

使用parseFloat方法Float.parseFloat(str)

Looks like you are trying to cast the String str to a float. 看起来您正在尝试将String str转换为float。 Probably you have a mistake and you are supposed to use the l variable instead. 可能您有一个错误,应该改用l变量。

Hm, sorry for my English 嗯,抱歉我的英语

If str is a String that you can't convert into a float like this (float)str , you must use a Float method called parseFloat(String s) , which returns a new float initialized to the value represented by the specified String 如果str是不能像这样的(float)str那样转换为float的String ,则必须使用一个名为parseFloat(String s)Float method ,该方法返回一个初始化为指定String表示的值的新float

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM