Is it possible to compare two numbers of type String to find which one is larger in java? (For example: 112 510,54 and 94 314,01) Thank you for your help!
-
Yes. It is possible. Please goahead. Ask us if you are stuck in middle. – Suresh Atta Jul 22 '15 at 11:42
-
yes it is. What have you tried? We won't just do your homework for you. – Dragondraikk Jul 22 '15 at 11:42
-
2You have to parse them into numbers and then compare them. – Fran Montero Jul 22 '15 at 11:44
-
1possible duplicate of [Converting String to int in Java?](http://stackoverflow.com/questions/5585779/converting-string-to-int-in-java) – Bertram Nudelbach Jul 22 '15 at 11:48
-
Yes, but comparing them without converting to an `int` will compare lexicographically, not by integer value. – MadConan Jul 22 '15 at 11:51
-
possible duplicate of [Compare Strings as if they were numbers](http://stackoverflow.com/questions/4356720/compare-strings-as-if-they-were-numbers) – MadConan Jul 22 '15 at 11:52
-
I tried to use substring, but the problem was the empty space that seperates the digits (every 3 digits); I was not able to parse into double while keeping the spaces in the numbers. However, for visibility issues I want to keep the spaces if possible. Below is the code that I wrote but that does not meet the requirements: ~Double.parseDouble(t.substring(0, t.indexOf(',')).replaceAll("\\W", ""));~ – J.M Jul 22 '15 at 12:06
6 Answers
You can convert the strings to Integers and then compare them like you compare two numbers.

- 27
- 7
Here's a comparator that will handle Numbers in strings of basically any number type. Information on how to use a Comparator is here and information on BigDecimal is here.
Edit: Modified it a little to handle your specific (european?) number format. Note that strings are immutable so your input values aren't actually changed at all.
public class NumberStringComparitor implements Comparator<String> {
public int compare(String o1, String o2) {
BigDecimal left = new BigDecimal(o1.replace(" ", "").replace(",", "."));
BigDecimal right = new BigDecimal(o2.replace(" ", "").replace(",", "."));
return left.compareTo(right);
}
}
Second Edit: Might also be worth your time to set up a Decimal Formatter to both format and parse your numbers: Here's an example that'll handle your format:
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setGroupingSeparator(' ');
symbols.setDecimalSeparator(',');
String pattern = "#,##0.###";
DecimalFormat decimalFormat = new DecimalFormat( pattern, symbols );
decimalFormat.setMaximumIntegerDigits(19);
decimalFormat.setMaximumFractionDigits(2);
System.out.println( decimalFormat.format( 12345.67 ) );
You could then just do
decimalFormat.parse(numberString).compareTo(otherNumberString)
Generally though -- I'd spend some time poking around the DecimalFormat documentation.
Also -- don't build your own parsers -- way to much work for way to little gain unless there's some sort of serious performance impact on the other side of this.

- 2,327
- 16
- 23
First do substring until comma and check which String is longer (bigger number) if lenghs are this same, compare single naumbers of string one by one. If one of them is bigger - have your answer.

- 642
- 5
- 16
-
Actually I tried to use substring, but the problem was the empty space that seperates the digits (every 3 digits); I was not able to parse into double while keeping the spaces in the numbers, however, for visibility issues I want to keep the spaces if possible. `Double.parseDouble(t.substring(0, t.indexOf(',')).replaceAll("\\W", ""));` – J.M Jul 22 '15 at 11:58
-
don't use double, because has low precision (random number on the end of byte representation). For example double d1=0.5; double d2=0.5; d1=d2 returns false. – Victor1125 Jul 24 '15 at 08:36
-
For get away with withe spaces use: `yourString.replaceAll("\\s+", "");` – Victor1125 Jul 24 '15 at 08:40
No replace, no conversion, no bigdecimals, no parse, works on any delimiter. Not perfect. Use at your own risk.
public class OtterApproach
{
public static void minmax(String s1, String s2)
{
String min = s1, max = s2;
if (s1.length() > s2.length())
{
min = s2;
max = s1;
}
else if (s1.length() == s2.length())
{
Character s1c = null;
Character s2c = null;
for (int i = 0; i < s1.length(); i++)
{
s1c = s1.charAt(i);
s2c = s2.charAt(i);
if ( s1c.equals(s2c)
|| ( !Character.isDigit(s1c)
&& !Character.isDigit(s2c)))
{
continue;
}
else if (s1c < s2c)
{
min = s1;
max = s2;
break;
}
else
{
max = s1;
min = s2;
break;
}
}
}
System.out.println("Min " + min + " max " + max);
}
public static void main (String[] args)
{
String s1 = "112 510,54";
String s2 = "94 314,01";
minmax(s1,s2);
minmax("1111 1111,00" , "2222 1111,00");
minmax("999 454,44", "999 454,43");
minmax("1,0", "2,0");
minmax("999 454,42", "999 454,43");
minmax("2,0", "1,0");
minmax("222.10", "222.09");
minmax("222,09", "222,09");
minmax("222,10", "222,09");
minmax("222.10", "222,09");
minmax("222,10", "222.09");
}
}
The output
Min 94 314,01 max 112 510,54
Min 1111 1111,00 max 2222 1111,00
Min 999 454,43 max 999 454,44
Min 1,0 max 2,0
Min 999 454,42 max 999 454,43
Min 1,0 max 2,0
Min 222.09 max 222.10
Min 222,09 max 222,09
Min 222,09 max 222,10
Min 222,09 max 222.10
Min 222.09 max 222,10

- 5,813
- 1
- 41
- 70
First to convert string into the double (because your examples are way too big to declare as integer), you don't want to have comma in the numbers
String stringWithoutComma = yourString.replaceAll(",", "");
Then parse to double using
double number = Double.parseDouble(stringWithoutComma);
Finally the comparison is possible.

- 11,216
- 8
- 63
- 98
You need to convert string values to the numbers first. The example values you have given contains commas so they can not be parsed using parseInt method. You need to use NumberFormat class instead as given below.
NumberFormat.getNumberInstance(java.util.Locale.US).parse("111,323,234")
This is required as the values you have provided contains commas.
Once you have two int values, compare them as normal numbers.

- 541
- 4
- 12