0

My goal is store BigDecimal values as I input them through the Scanner class. I want to store BigDecimal numbers into an array so that I can sort them numerically. Can this be done using a single-dimensional array?

I can't find any BigInteger methods designed for this task. I want BigDecimal values because the decimal precision is important to me.

poetryrocksalot
  • 697
  • 2
  • 10
  • 19

2 Answers2

1

If you want to sort them easily, you could do the following:

    BigDecimal x1 = new BigDecimal("1235.2345");
    BigDecimal x2 = new BigDecimal("235.2345");
    BigDecimal[] nums = {x1,x2};
    List<BigDecimal> lnums = Arrays.asList(nums);
    Collections.sort(lnums);

Arrays.asList() merely passes the value of the reference of your array. Collections.sort() uses merge sort.

Steve P.
  • 14,489
  • 8
  • 42
  • 72
0

I hope this help you,

BigDecimal x = new BigDecimal(5.94839);
BigDecimal y = new BigDecimal(9.03937);
BigDecimal[] myBDArr = {x,y};
System.out.println(myBDArr[0]);
System.out.println(myBDArr[1]);

Output:

5.948389999999999844249032321386039257049560546875
9.0393699999999999050714905024506151676177978515625
Steve P.
  • 14,489
  • 8
  • 42
  • 72
Naveen
  • 367
  • 2
  • 4
  • 11