Skip to content

Commit

Permalink
Added BigDecimal
Browse files Browse the repository at this point in the history
  • Loading branch information
javasks committed Mar 16, 2020
1 parent 6114deb commit 63e7add
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/com/sks/hkr/java/bigNumber/BigDecimalProblem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.sks.hkr.java.bigNumber;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class BigDecimalProblem {

public static void main(String[] args) {
//Input
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[] s = new String[n + 2];
for (int i = 0; i < n; i++) {
s[i] = sc.next();
}
sc.close();

// We want to sort in descending order while preserving the contents
// of each String. A comparator can achieve this for us. We convert
// to BigDecimal inside our comparator so that the change is not
// permanent and our String's form for each number is preserved.
Comparator<String> customComparator = new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
BigDecimal a = new BigDecimal(s1);
BigDecimal b = new BigDecimal(s2);
return b.compareTo(a); // descending order
}
};

Arrays.sort(s, 0, n, customComparator);

//Output
for (int i = 0; i < n; i++) {
System.out.println(s[i]);
}
}
}

0 comments on commit 63e7add

Please sign in to comment.