如何为null float类型的变量 [英] how to null a variable of type float

查看:817
本文介绍了如何为null float类型的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code是工作,如果我的浮点变量设置为0,但我希望我的code工作,如果我的变量为空。我怎么能这样做呢?这里是一个code片段我写的:

 浮动oikonomia =(浮点)((浮点)新的浮动(vprosvasis7.getText()的toString()));                         如果(oikonomia == 0){

我已经追平如果(oikonomia == NULL)如果(oikonomia = NULL)但它不是工作。

P.S:另一种方式是到最初设置 oikonomia = 0; ,如果用户改变它,去我如果会话。这不是任何工作。

浮法oikonomia = Float.valueOf(vprosvasis7.getText()的toString());

 如果(oikonomia.toString()==){浮动oikonomia =(浮点)((浮点)新的浮动(vprosvasis7.getText()的toString()));
                  oikonomia = 0;
                         如果(oikonomia == 0){

这样是不是工作压力太大:

 浮法oikonomia = Float.valueOf(vprosvasis7.getText()的toString());                         如果(oikonomia.toString()==){


解决方案

如果你想有一个可为空的浮动,然后尝试浮动而不是浮动

 浮动oikonomia =新的浮动(vprosvasis7.getText()的toString());

但它永远不会是这样在您的示例...

修改:AAAAH,我开始理解你的问题是什么。你真的不知道是什么 vprosvasis7 包含,以及是否正确初始化为一个数字。因此,尝试这一点,那么:

 浮动oikonomia = NULL;尝试{
  oikonomia =新的浮动(vprosvasis7.getText()的toString());
}
赶上(例外忽略){
  //我们忽略了潜在的例子例外。
  //清洁解决方案会像对待NumberFormatException异常
  //和NullPointerException异常这里
}//出现这种情况时,我们以前有一个例外
如果(oikonomia == NULL){
  // [...]
}//这发生在上述浮被正确解析
其他{
  // [...]
}

当然也有很多方法来简化上面写清洁code。但我认为这应该对解释你应该如何正确,安全地解析字符串浮动不运行任何异常...

My code is working if my float variable is set to 0, but I want my code to work if my variable is null. How could I do that? Here is a code snippet I wrote:

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

                         if(oikonomia==0){

I have tied if(oikonomia==null) or if(oikonomia=null) but it is not working.

P.S.: Another way would be to initially set oikonomia=0; and, if the users change it, go to my if session. This is not working either.

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

                     if(oikonomia.toString() == " "){



float oikonomia= (float)((float)new Float(vprosvasis7.getText().toString()));
                  oikonomia=0;
                         if(oikonomia==0){

that way is not working too:

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

                         if(oikonomia.toString() == " "){

解决方案

If you want a nullable float, then try Float instead of float

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

But it will never be null that way as in your example...

EDIT: Aaaah, I'm starting to understand what your problem is. You really don't know what vprosvasis7 contains, and whether it is correctly initialised as a number. So try this, then:

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 {
  // [...]
}

Of course there are lots of ways to simplify the above and write cleaner code. But I think this should about explain how you should correctly and safely parse a String into a float without running into any Exceptions...

这篇关于如何为null float类型的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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