4

I've two strings which I need to compare, but I want to compare them meaningfully such that when they are numbers their actual value should be compared. So far I've tried the following solution:

String str1 = "-0.6";
String str2 = "-.6";
if (NumberUtils.isNumber(str1) && NumberUtils.isNumber(str2)) {
    Number num1 = NumberUtils.createNumber(str1);
    Number num2 = NumberUtils.createNumber(str2);
    System.out.println(num1.equals(num2));
} else {
    System.out.println(str1.equals(str2));
}

This works as both are converted to doubles.
But this won't work in this case where:

String str1 = "6";
String str2 = "6.0";

Is there any easy way to do this, or will I have to write my own Comparator?

4
  • And how do you act when one string is not a number of some sort? Commented Jul 21, 2015 at 6:31
  • Can't you make the createNumber() method to always return Double for numbers? Commented Jul 21, 2015 at 6:32
  • This means that string is not meant for numerical comparisons such as this. You should go with convrting to double and compare them as you have mentioned. Commented Jul 21, 2015 at 6:32
  • Does it solve using something like Number num1 = 0. + NumberUtils.createNumber(str1);? The same for num2. Commented Jul 21, 2015 at 6:36

4 Answers 4

3

Instead of using the general-purpose createNumber(String), force them to doubles using createDouble(String):

String str1 = "-0.6";
String str2 = "-.6";
if (NumberUtils.isNumber(str1) && NumberUtils.isNumber(str2)) {
    Double d1 = NumberUtils.createDouble(str1);
    Double d2 = NumberUtils.createDouble(str2);
    System.out.println(d1.equals(d2));
} else {
    System.out.println(str1.equals(str2));
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can probably use BigDecimal for this:

final BigDecimal b1 = new BigDecimal(str1);
final BigDecimal b2 = new BigDecimal(str2);
return b1.compareTo(b2) == 0;

4 Comments

Why big decimal for such smaller numbers that are mentioned in the code snippet ?
what happens if one the strings is not a number?
@DegenSharew I mentioned that first thing in the comments
@sakthisundar because precision; at least, with a BigDecimal you don't get any surprises!
2

You could use Double.parseDouble(String) instead. It throws the NumberFormatException, which can be caught to inform you that you should not compare them as numbers.

try {
    Double d1 = Double.parseDouble(str1);
    Double d2 = Double.parseDouble(str2);

    //Now you have your doubles
} catch (NumberFormatException e)
{
    //Could not compare as numbers, do something else
}

Comments

2

As suggested on the comment one of the string could be a number and the other not or vice versa or both could be a string so instead of checking if the strings are number strings I will go using try catch block.

Like this:

try{
    Double d1 = Double.parseDouble(str1);
    Double d2 = Double.parseDouble(str2);
    System.out.println(d1.equals(d2));
}catch(NumberFormatException ex){

    System.out.println(num1.equals(num2));

}

This first tries to compare them as numbers but if at least one of them is not a number string it will fallback to string comparison in the catch block.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.