[英]Check for a valid double in Java
I want to check if a String
contains a Double
not an Integer
. 我想检查一个String
包含Double
而不是Integer
。 I am working in this way; 我以这种方式工作;
private boolean isDouble(String str) {
try {
Double.parseDouble(str);
return true;
}
catch(NumberFormatException e) {
return false;
}
}
For checking it, i simply passed; 为了检查,我只是通过了;
isDouble("123");
But it is not working, giving true
in both conditions ("123" ,"123.99"). 但是它不起作用,在两种情况下都为true
(“ 123”,“ 123.99”)。 What is wrong here? 怎么了
If you want to check that it is a number that does not fit into in Integer, you may round the double. 如果要检查它是否为整数不适合的数字,可以四舍五入。 Eg exploit the fact that round(1.2) != 1.2
, but round(1) == 1
. 例如,利用round(1.2) != 1.2
,而round(1) == 1
的事实。
private boolean isDouble(String str) {
try {
// check if it can be parsed as any double
double x = Double.parseDouble(str);
// check if the double can be converted without loss to an int
if (x == (int) x)
// if yes, this is an int, thus return false
return false;
// otherwise, this cannot be converted to an int (e.g. "1.2")
return true;
// short version: return x != (int) x;
}
catch(NumberFormatException e) {
return false;
}
}
You could use Scanner(String) and use the hasNextDouble() method. 您可以使用Scanner(String)并使用hasNextDouble()方法。 From its javadoc: 从其javadoc:
Returns true if the next token in this scanner's input can be interpreted as a double value using the nextDouble() method. 如果此扫描程序输入中的下一个标记可以使用nextDouble()方法解释为双精度值,则返回true。 For example: 例如:
if(source.contains(".")){
Scanner scanner = new Scanner(source);
boolean isDouble = scanner.hasNextDouble();
return isDouble;
}
return false;
You can also always start by parsing to double
, and then test if the double
is an int
or not. 您也可以始终先将其解析为double
,然后测试double
是否为int
。
private void main() {
String str = "123";
Double value = parseDouble(str);
boolean isInt = isInt(value);
}
private void isInt(Double value) {
if(value != null) {
return (value == (int) value) ? true : false;
}
return false;
}
private double parseToDouble(String str) {
Double value = null;
try {
value = Double.parseDouble(str);
}
catch(NumberFormatException e) {
// Do something
}
return value;
}
The problem is due to the fact that 1.00 is 1 and it's a double . 问题是由于1.00是1并且它是一个double的事实。 This means you can't simply parse a double
and pretend that the code detect itself if it's an int
or not. 这意味着您不能简单地解析一个double
并假装该代码检测到自己是否为int
。 For this you should add a check, I think the easiest is the following: 为此,您应该添加一个检查,我认为最简单的方法如下:
private boolean isDouble(String str) {
try {
double myDouble = Double.parseDouble(str);
myDouble -= (int)myDouble; //this way you are making the (for example) 10.3 = 0.3
return myDouble != (double)0.00; //this way you check if the result is not zero. if it's zero it was an integer, elseway it was a double
}
catch(NumberFormatException e) {
return false;
}
}
I did it without editor, so tell me if something is wrong. 我是在没有编辑器的情况下完成的,所以请告诉我是否有问题。
Hope this helps 希望这可以帮助
Check the simple code 检查简单代码
private boolean isDecimalPresent(d){
try {
return d%1!=0;
}
catch(NumberFormatException e) {
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.