Once in while in java we might have to check If a passed string to method is a valid numerical value. Below are some utility methods to accomplish this:
As you guys might have noticed that I have an assert statement for both the methods which checks the method against string "NaN". The thing which makes this more interesting is that the method which uses NumberFormat returns false for this string while the method which uses Double.parseDouble(string) returns true.
You might say that the method which uses NumberFormat is correct because "NaN" is technically just a mere string value. Below is the description of how java treats "NaN" while doing floating-point operations :
"NaN" stands for "not a number". "Nan" is produced if a floating point operation has some input parameters that cause the operation to produce some undefined result. For example, 0.0 divided
by 0.0 is arithmetically undefined. Taking the square root of a negative number is also undefined.
That's all for now folks stay tuned for new posts !!!!!!!!
Using java.text.NumberFormat.
public static boolean isNumeric(final String str) {
try {
Double.parseDouble(str);
} catch (final NumberFormatException | NullPointerException e) {
return false;
}
return true;
}
try {
Double.parseDouble(str);
} catch (final NumberFormatException | NullPointerException e) {
return false;
}
return true;
}
JUnit Test Case:
Assert.assertEquals(false, yourClass.isNumeric(null));
Assert.assertEquals(false, yourClass.isNumeric(""));
Assert.assertEquals(true, yourClass.isNumeric("2.74"));
Assert.assertEquals(true, yourClass.isNumeric("2.745435E3"));
Assert.assertEquals(true, yourClass.isNumeric("0"));
Assert.assertEquals(true, yourClass.isNumeric("0.0f"));
Assert.assertEquals(false, yourClass.isNumeric("NaN"));
Assert.assertEquals(false, yourClass.isNumeric(""));
Assert.assertEquals(true, yourClass.isNumeric("2.74"));
Assert.assertEquals(true, yourClass.isNumeric("2.745435E3"));
Assert.assertEquals(true, yourClass.isNumeric("0"));
Assert.assertEquals(true, yourClass.isNumeric("0.0f"));
Assert.assertEquals(false, yourClass.isNumeric("NaN"));
Using Double.parseDouble.
public static boolean isNumeric(final String string) {
try {
NumberFormat.getInstance().parse(string);
} catch (ParseException | NullPointerException e) {
return false;
}
return true;
}
try {
NumberFormat.getInstance().parse(string);
} catch (ParseException | NullPointerException e) {
return false;
}
return true;
}
JUnit Test Case:
Assert.assertEquals(false, yourClass.isNumeric(null));
Assert.assertEquals(false, yourClass.isNumeric(""));
Assert.assertEquals(true, yourClass.isNumeric("2.74"));
Assert.assertEquals(true, yourClass.isNumeric("2.745435E3"));
Assert.assertEquals(true, yourClass.isNumeric("0"));
Assert.assertEquals(true, yourClass.isNumeric("0.0f"));
Assert.assertEquals(true, yourClass.isNumeric("NaN"));
Assert.assertEquals(false, yourClass.isNumeric(""));
Assert.assertEquals(true, yourClass.isNumeric("2.74"));
Assert.assertEquals(true, yourClass.isNumeric("2.745435E3"));
Assert.assertEquals(true, yourClass.isNumeric("0"));
Assert.assertEquals(true, yourClass.isNumeric("0.0f"));
Assert.assertEquals(true, yourClass.isNumeric("NaN"));
As you guys might have noticed that I have an assert statement for both the methods which checks the method against string "NaN". The thing which makes this more interesting is that the method which uses NumberFormat returns false for this string while the method which uses Double.parseDouble(string) returns true.
You might say that the method which uses NumberFormat is correct because "NaN" is technically just a mere string value. Below is the description of how java treats "NaN" while doing floating-point operations :
"NaN" stands for "not a number". "Nan" is produced if a floating point operation has some input parameters that cause the operation to produce some undefined result. For example, 0.0 divided
by 0.0 is arithmetically undefined. Taking the square root of a negative number is also undefined.
That's all for now folks stay tuned for new posts !!!!!!!!
No comments:
Post a Comment