[英]Is it not possible to store BigDecimal values in a single-dimensional array
My goal is store BigDecimal values as I input them through the Scanner class. 我的目标是在通过Scanner类输入BigDecimal值时存储它们。 I want to store BigDecimal numbers into an array so that I can sort them numerically. 我想将BigDecimal数字存储到数组中,以便可以对其进行数字排序。 Can this be done using a single-dimensional array? 可以使用一维数组吗?
I can't find any BigInteger methods designed for this task. 我找不到为此任务设计的任何BigInteger方法。 I want BigDecimal values because the decimal precision is important to me. 我需要BigDecimal值,因为十进制精度对我很重要。
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. Arrays.asList()
仅传递数组引用的值。 Collections.sort()
uses merge sort. Collections.sort()
使用合并排序。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.