简体   繁体   English

无法将BigDecimal值存储在一维数组中

[英]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.

相关问题 显示结果的一维数组问题 - Single-dimensional array issue with displaying results 如何将两个一维数组值存储到 jdbc java bean 值中并将其存储到 mysql 中? - how to store two single dimensional array values into jdbc java bean values and stored it into mysql? 将字符存储在二维数组中而不是 ASCII 值 - Store characters in a 2 dimensional array instead of ASCII values 是否可以通过JPA在Oracle中存储BigDecimal 0.000 - Is it possible to store BigDecimal 0.000 in Oracle with JPA 是否可以在单个数组中存储多个多维数组? - Is it possible to store multiple multidimensional arrays in a single array? Java-如何查找一维数组中所有值的排列并将它们存储在二维数组中 - Java- How to find the permutation of all values in a 1 dimensional array and store them in a 2 dimensional array 如何使用扫描仪读取文件并将值存储在二维数组中 - how to Read a file and store the values in a 2-Dimensional Array using Scanner 二维数组作为输入; 存储在自己的二维数组中 - 2 dimensional array as input; store in own 2 dimensional array 在Java中将二维数组与单维数组进行比较 - Comparing a 2 dimensional array to a single dimensional array in Java 处理一维数组Java - processing single dimensional array java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM