首页 > 编程知识 正文

java怎么定义一个float型变量,java正确的float变量的声明

时间:2023-05-03 12:15:24 阅读:270129 作者:3077

如果你想要一个可以为空的浮点数,那么试试Float而不是float

Float oikonomia= new Float(vprosvasis7.getText().toString());

但它永远不会像你的例子那样是空的……

编辑:Aaaah,我开始明白你的问题了.你真的不知道vprosvasis7包含什么,以及它是否正确初始化为数字.所以试试这个,然后:

Float oikonomia = null;

try {

oikonomia = new Float(vprosvasis7.getText().toString());

}

catch (Exception ignore) {

// We're ignoring potential exceptions for the example.

// Cleaner solutions would treat NumberFormatException

// and NullPointerExceptions here

}

// This happens when we had an exception before

if (oikonomia == null) {

// [...]

}

// This happens when the above float was correctly parsed

else {

<一分快三计划稳赚would treat NumberFormatException

// and NullPointerExceptions here

}

// This happens when we had an exception before

if (oikonomia == null) {

// [...]

}

// This happens when the above float was correctly parsed

else {

// [...]

}

当然,有很多方法可以简化上述内容并编写更清晰的代码.但我认为这应该解释如何正确安全地将String解析为float而不会遇到任何异常……

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。