Java:(int)(float)Float.valueOf(s)或Float.valueOf().toInt() [英] Java: (int)(float)Float.valueOf(s) or Float.valueOf(s).toInt()

查看:617
本文介绍了Java:(int)(float)Float.valueOf(s)或Float.valueOf().toInt()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚碰到这个小问题,我想要其他人的输入在这



我是在徘徊什么是最好的解决方案将一个字符串转换为一个int

 (int)(float)Float.valueOf(s)

  Float.valueOf(s).toInt 

s是从文本字段输入的字符串,因此我不能保证它一定是int

我的直觉是双重演员是丑陋的,应该避免



解决方案

您的要求不清楚:




  • 如果你期望一个整数,并且不希望允许用户输入一个带有小数点的数字,只需使用 Integer.valueOf(String) Integer.parseInt(String)并捕获 NumberFormatException


  • 如果要允许带小数点的数字,请使用 Float.valueOf(String) Float.parseFloat String)




    • 如果您只想截断 float c $ c> int ,则 Float.intValue()或两个类型是等价的。 ( intValue 的javadoc明确说明了这一点。)


    • 如果要舍入到最接近的 int ,使用 Math.round()而不是转换。





无论采取什么方法,都应该捕获 NumberFormatException 用户可以输入不是有效的10进制数(具有或不具有小数点)...或超过数字类型的边界的垃圾。



(我想你可以使用正则表达式或其他东西检查字符串之前,尝试转换它,但它是更容易只是让异常发生和处理它。异常效率问题不可能是一个实际问题在这种使用-case。)






关于 intValue()比两个cast更好:这是一个风格问题。这两种方法做同样的事情...根据javadoc。有可能一个比另一个稍微更有效,但是:


  1. 这不应该是你的使用情况和

  2. ,唯一可以确定的方法是在执行平台上配置两个备选项...坦白说,这不值得付出。


I just bumped into this little problem and I wanted the input of other people on this

I was wandering what was the best solution to convert a String to an int

(int)(float)Float.valueOf(s) 

or

Float.valueOf(s).toInt()

s is a String inputed from a textfield so I can not guarantee that it is necessarily an int

my instincts is that the double cast is ugly and should be avoided

Your input?

解决方案

Your requirements are unclear:

  • If you are expecting an integer and don't want to allow the user to enter a number with a decimal point in it, simply use Integer.valueOf(String) or Integer.parseInt(String) and catch the NumberFormatException.

  • If you want to allow numbers with decimal points, then use Float.valueOf(String) or Float.parseFloat(String).

    • If you simply want to truncate the float to an int then either Float.intValue() or two casts are equivalent. (The javadoc for intValue explicitly states this.)

    • If you want to round to the nearest int, use Math.round() instead of a cast.

You should catch NumberFormatException whatever approach you take, since the user could enter rubbish that is not a valid base-10 number (with or without a decimal point) ... or that exceeds the bounds of the number type.

(I suppose that you could use a regex or something to check the String before trying to convert it, but it is simpler to just let the exception happen and deal with it. The exception efficiency issue is unlikely to be a practical concern in this use-case.)


On your original question as to whether intValue() is better than two casts: it is a matter of style. The two approaches do the same thing ... according to the javadoc. It is possible that one will be slightly more efficient than the other, but:

  1. that shouldn't be a concern for your use-case, and
  2. the only way to know for sure would be to profile the two alternatives on your execution platform ... and frankly it is not worth the effort.

这篇关于Java:(int)(float)Float.valueOf(s)或Float.valueOf().toInt()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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