Java:(int)(float)Float.valueOf(s) 或 Float.valueOf(s).toInt()
我刚刚遇到这个小问题,我想要其他人对此的输入,
我在想将字符串转换为 int 的最佳解决方案是什么
(int)(float)Float.valueOf(s)
,或者
Float.valueOf(s).toInt()
s 是从文本字段输入的字符串,所以我不能保证它一定是 我的直觉
是双重强制转换很丑陋,应该避免
您的意见?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的要求不清楚:
如果您需要一个整数并且不希望用户输入带小数点的数字,只需使用
Integer.valueOf(String)
或Integer.parseInt(String)
并捕获NumberFormatException
。如果您想允许带有小数点的数字,请使用
Float.valueOf(String)
或Float.parseFloat(String)
。如果您只是想将
float
截断为int
,则Float.intValue()
或两次强制转换是等效的。 (intValue
的 javadoc 明确说明了这一点。)如果您想舍入到最接近的
int
,请使用Math.round()
而不是强制转换。无论您采取什么方法,您都应该捕获
NumberFormatException
,因为用户可能会输入不是有效的 10 进制数字(带或不带小数点)的垃圾……或者超出数字的范围类型。(我想您可以在尝试转换字符串之前使用正则表达式或其他东西来检查字符串,但是让异常发生并处理它会更简单。异常效率问题不太可能成为这种使用中的实际问题-case.)
关于您最初的问题,即
intValue()
是否比两次转换更好:这是一个风格问题。根据javadoc,这两种方法做同样的事情。一个可能会比另一个稍微高效一些,但是: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)
orInteger.parseInt(String)
and catch theNumberFormatException
.If you want to allow numbers with decimal points, then use
Float.valueOf(String)
orFloat.parseFloat(String)
.If you simply want to truncate the
float
to anint
then eitherFloat.intValue()
or two casts are equivalent. (The javadoc forintValue
explicitly states this.)If you want to round to the nearest
int
, useMath.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:您应该使用 Integer.valueOf(..) 并捕获 NumberFormatException(如果字符串无法解析为整数)。
You should use
Integer.valueOf(..)
, and catch theNumberFormatException
(if the string cannot be parsed as an integer).Integer.parseInt(string) 是最好的
最好在调用之前检查字符串是否是 int 。
Integer.parseInt(string) would be best
Would be best to check if the string is an int before calling this.
正如其他人指出的那样,这只是样式问题而不是性能问题......但是如果您担心性能,您应该使用正则表达式通过 javascript 验证浏览器本身中的文本字段数据,
该正则表达式只允许将整数提交到后端代码,从而通过避免一次网络旅行来提高性能。您仍然应该在后端验证数据,因为您可以使用
As others pointed out, its just matter of style not performance... but if you are worried about performance you should validate the text field data in browser itself by javascript using a regex
which would allow only integers to be submitted to your back-end code and hence improving performance by avoiding one network trip. Still you should validate the data in back-end, for that you can use