This question already has an answer here:
这个问题在这里已有答案:
- Double toString or String.valufOf not returning right value [duplicate] 1 answer
- Double toString或String.valufOf没有返回正确的值[复制] 1个答案
Double dbl = 0.003;
String str = dbl.toString();
str.matches("^\\d{1,7}\\.?\\d{1,3}$");
regex
returns false
.
正则表达式返回false。
str
value returns as 0.0030
str值返回0.0030
Regex is failing which is different from Float problem.
正则表达式失败,这与Float问题不同。
2 个解决方案
#1
1
It is indeed behaving correctly (the regex bit I mean). You've limited the digits to three following the decimal and you're giving it four.
它确实表现正常(我的意思是正则表达式)。你已经将数字限制在小数点后面的三位数,你给它四位数。
I'd suggest either modifying the string to remove trailing zeros (assuming they're after a decimal point of course), or changing the regex to make it suitable.
我建议修改字符串以删除尾随零(假设它们当然是在小数点之后),或者更改正则表达式以使其适合。
The former can be done with something like:
前者可以通过以下方式完成:
s = s.replaceFirst("(\\.\\d+)0+$", "$1").replaceFirst("\\.0+$", ".0");
The latter involves adding zeros to the end of the regex:
后者涉及在正则表达式的末尾添加零:
str.matches("^\\d{1,7}\\.?\\d{1,3}0*$");
You may also want to consider putting the decimal and fractional digits into a group, regardless of which method you choose. In other words, something like:
您可能还需要考虑将小数位和小数位放入一个组中,无论您选择哪种方法。换句话说,类似于:
str.matches("^\\d{1,7}(\\.\\d{1,3})?$");
As it stands now, that regex will also accept a 10-digit integral number.
就目前而言,正则表达式也将接受10位整数。
#2
1
As I pointed out in your other question, you are just running into the known bug-
正如我在你的另一个问题中指出的那样,你刚刚遇到已知的错误 -
http://bugs.java.com/view_bug.do?bug_id=4428022
http://bugs.java.com/view_bug.do?bug_id=4428022
0050 is not 3-character long. Hence, false. You can try updating your JDK environment or use printf. In my JDK7, I get-
0050不是3个字符长。因此,错误。您可以尝试更新JDK环境或使用printf。在我的JDK7中,我得到 -
0.005
0.005
true
真正
#1
1
It is indeed behaving correctly (the regex bit I mean). You've limited the digits to three following the decimal and you're giving it four.
它确实表现正常(我的意思是正则表达式)。你已经将数字限制在小数点后面的三位数,你给它四位数。
I'd suggest either modifying the string to remove trailing zeros (assuming they're after a decimal point of course), or changing the regex to make it suitable.
我建议修改字符串以删除尾随零(假设它们当然是在小数点之后),或者更改正则表达式以使其适合。
The former can be done with something like:
前者可以通过以下方式完成:
s = s.replaceFirst("(\\.\\d+)0+$", "$1").replaceFirst("\\.0+$", ".0");
The latter involves adding zeros to the end of the regex:
后者涉及在正则表达式的末尾添加零:
str.matches("^\\d{1,7}\\.?\\d{1,3}0*$");
You may also want to consider putting the decimal and fractional digits into a group, regardless of which method you choose. In other words, something like:
您可能还需要考虑将小数位和小数位放入一个组中,无论您选择哪种方法。换句话说,类似于:
str.matches("^\\d{1,7}(\\.\\d{1,3})?$");
As it stands now, that regex will also accept a 10-digit integral number.
就目前而言,正则表达式也将接受10位整数。
#2
1
As I pointed out in your other question, you are just running into the known bug-
正如我在你的另一个问题中指出的那样,你刚刚遇到已知的错误 -
http://bugs.java.com/view_bug.do?bug_id=4428022
http://bugs.java.com/view_bug.do?bug_id=4428022
0050 is not 3-character long. Hence, false. You can try updating your JDK environment or use printf. In my JDK7, I get-
0050不是3个字符长。因此,错误。您可以尝试更新JDK环境或使用printf。在我的JDK7中,我得到 -
0.005
0.005
true
真正