Java BigDecimal

Sort by

recency

|

513 Discussions

|

  • + 0 comments
    String[] sortedArray = Arrays.stream(s)
                                 .filter(Objects::nonNull) 
                                 .sorted((a, b) -> (new BigDecimal(b)).compareTo(new BigDecimal(a)))
                                 .toArray(String[]::new);
            for(int i=0;i<n;i++)
            {
                s[i]=sortedArray[i];
            } 
    
  • + 0 comments

    import java.io.; import java.util.; import java.math.*;

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String arr [] = new String[n];
        for(int i = 0; i < n; i++){
            arr[i] = sc.next();
        }
        sc.close();
    
        Arrays.sort(arr,(String a,String b)-> new BigDecimal(b).compareTo(new BigDecimal(a)));
    
        for(int i = 0; i < n; i++){
            System.out.println(arr[i]);
            }
    
    }
    

    }

  • + 1 comment

    Hello friends! I'm at this problem and try to figure out how ti solve it. i didn't pass 3 tests and I which what those are. The main problom for me is when i convert from string array to bigdecimals array some values suffering modifications like this : string array -> ".12" ==> to bigDecimals -> "0.12" and i don't want to happend. I've searched all internet but nothing can helped me. Do you have any advice? Thank you!

  • + 0 comments
            for (int i=n; i>=0; i--) {
                for (int j=0; j<i-1; j++) {
                    BigDecimal numA = new BigDecimal(s[j]);
                    BigDecimal numB = new BigDecimal(s[j+1]);
                    
                    if (numA.compareTo(numB) == -1) {
                        String tmp = s[j];
                        
                        s[j] = s[j+1];
                        s[j+1] = tmp;
                    }
                }
            }
    
  • + 0 comments

    import java.math.BigDecimal; import java.util.*; class Solution{ 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

        Arrays.sort(s, 0, n, new Comparator<String>() {
            public int compare(String a, String b) {
                BigDecimal A = new BigDecimal(a);
                BigDecimal B = new BigDecimal(b);
                return B.compareTo(A);
            }
        });
    
        //Output
        for(int i=0;i<n;i++)
        {
            System.out.println(s[i]);
        }
    }
    

    }