字符串转双精度Java错误

发布于 2024-11-28 04:38:43 字数 326 浏览 0 评论 0原文

我在使用 Spring 和 Java 将字符串解析为双精度数时遇到了一些问题。这与 Spring 并不完全相关,但它可能会让你陷入困境。

我有一个 CustomNumberEditor,用于轻松将字符串解析为双精度数。 java.text.DecimalFormat 用作将双精度数解析为字符串的十进制格式。但是,当您执行 numberFormat.parse(stringDouble) 时,如果字符串以数字开头,后跟字母,则返回的值是数字。即 12a 被解析为 12。对我来说,这显然是错误的,我想轻松解决这个问题。

我想这应该以其他类型的数字格式、属性或其他东西来实现,但我找不到它。有什么想法吗?

I'm having some problems in the moment of parsing String to Doubles, with Spring and Java. This is not totally related to Spring, but it may put you in situation.

I had a CustomNumberEditor, for parsing easily Strings to Doubles.
java.text.DecimalFormat was used as the decimal format for parsing double to string. However, when you do numberFormat.parse(stringDouble) if the string starts with numbers, and follow with letters, the value returned is the numbers. i.e. 12a is parsed to 12. For me this is clearly and error, and I would like to solve this easily.

I imagine this should be implemented in some other kind of numberFormat, or attributes or something, but I could not find it. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

做个ˇ局外人 2024-12-05 04:38:43

您应该使用 采用 ParsePosition 作为参数的解析方法版本。解析完成后,该位置的索引将更新为最后使用的字符之后的索引。因此,您可以检查该索引是否等于字符串的长度。如果不是,则意味着整个字符串尚未用于生成返回的数字。

You should use the version of the parse method taking a ParsePosition as argument. Once the parsing is done, the index of the position is updated to the index after the last character used. You can thus check that this index is equal to the length of the string. If it's not, then it means that the whole string has not been used to produce the returned number.

夜灵血窟げ 2024-12-05 04:38:43
try{
    double d = Double.parseDouble("12abc");
}
catch(NumberFormatException nfe){
    System.out.println("The string is not formatted correctly");
    //do something here if the string is bad

正如

上面有人所说,使用 Double.parseDouble(String s)。如果字符串格式不正确,它将抛出异常。然后,您只需将其包装在 try-catch 语句中,并在输入格式不正确的情况下执行您希望程序执行的操作。

try{
    double d = Double.parseDouble("12abc");
}
catch(NumberFormatException nfe){
    System.out.println("The string is not formatted correctly");
    //do something here if the string is bad

}

As someone said above, use Double.parseDouble(String s). It will throw an exception if the string is not formatted properly. Then you just wrap that in a try-catch statement and do whatever it is you want your program to do in the event that the input is improperly formatted.

风流物 2024-12-05 04:38:43

parse(...) 方法不会消耗所有文本,因此将允许像 12a 等“数字”(这也可能是像 12a 这样的货币) >12 美元)。

但是,您的 CustomNumberEditor 可能只是将仅数字模式应用于 String#matches(...) ,以便在解析字符串之前检查该字符串仅包含数字 - 某些内容例如 ^-?\d+\.?\d*$ (字符串只能包含数字、减号和分隔符)。但您应该注意,这取决于区域设置,并且不如十进制格式模式灵活。

The parse(...) method won't consume all the text and thus would allow "numbers" like 12a etc. (this might also be a currency like 12USD).

Your CustomNumberEditor might, however, just apply a number only pattern to String#matches(...) in order to check the string only contains a number before parsing it - something like ^-?\d+\.?\d*$ (String must only contain digits, a minus sign and a separator) . You should note however, that this is locale dependent and not as flexible as the decimal format pattern.

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