gpt4 book ai didi

Java 文本字段十进制验证

转载 作者:行者123 更新时间:2023-12-01 06:20:03 25 4
gpt4 key购买 nike

如何验证用户输入仅包含 5 位数字和 2 位小数。我正在使用下面的代码来检查 5 位数字,但如何检查小数点后 2 位。

if (!id.equals("")) {
try {
Integer.parseInt(id);
if (id.length() <= 5) {
return true;
}
} catch (NumberFormatException nfe) {
return false;
}
}

最佳答案

DecimalFormat类(class)将在这方面对您有很大帮助。您可以检查用户输入是否可以根据特定模式进行解析。

commons-validator library wraps this up in a convenient method call .

关于Java 文本字段十进制验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7904995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com