Reputation: 85
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!
Upvotes: 0
Views: 1934
Reputation: 702
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.
Upvotes: 0
Reputation: 6119
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
Upvotes: 0
Reputation: 579
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.
Upvotes: 0
Reputation: 11786
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.
Upvotes: 0
Reputation: 2416
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.
Upvotes: 1
Reputation: 27
You can convert the strings to Integers and then compare them like you compare two numbers.
Upvotes: 1