Java:(int)(float)Float.valueOf(s) 或 Float.valueOf(s).toInt()

发布于 2024-11-06 21:49:42 字数 261 浏览 1 评论 0原文

我刚刚遇到这个小问题,我想要其他人对此的输入,

我在想将字符串转换为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

记忆消瘦 2024-11-13 21:49:42

您的要求不清楚:

  • 如果您需要一个整数并且不希望用户输入带小数点的数字,只需使用 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,这两种方法做同样的事情。一个可能会比另一个稍微高效一些,但是:

  1. 这不应该成为您的用例的问题,
  2. 唯一确定的方法是在您的执行平台上分析这两种替代方案。坦率地说,这不值得付出努力。

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.
篱下浅笙歌 2024-11-13 21:49:42

您应该使用 Integer.valueOf(..) 并捕获 NumberFormatException(如果字符串无法解析为整数)。

You should use Integer.valueOf(..), and catch the NumberFormatException (if the string cannot be parsed as an integer).

反差帅 2024-11-13 21:49:42

Integer.parseInt(string) 是最好的

int x = Integer.parseInt(s);

最好在调用之前检查字符串是否是 int 。

Integer.parseInt(string) would be best

int x = Integer.parseInt(s);

Would be best to check if the string is an int before calling this.

千里故人稀 2024-11-13 21:49:42

正如其他人指出的那样,这只是样式问题而不是性能问题......但是如果您担心性能,您应该使用正则表达式通过 javascript 验证浏览器本身中的文本字段数据,

^[0-9]*$

该正则表达式只允许将整数提交到后端代码,从而通过避免一次网络旅行来提高性能。您仍然应该在后端验证数据,因为您可以使用

Integer.parseInt(String s) throws NumberFormatException

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

^[0-9]*$

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

Integer.parseInt(String s) throws NumberFormatException
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文