【问题标题】:Convert String value into BigDecimal and sort them in descending order and then print them将 String 值转换为 BigDecimal 并按降序对它们进行排序,然后打印它们
【发布时间】:2020-10-31 05:20:07
【问题描述】:

这是我的代码,但它没有像 BigDecimal 变量那样采用字符串

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

public class BigDesimal {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String[] s = new String[n];
        for(int i=0; i<n; ++i) {
            s[i] = in.next();
        }
        in.close();

        BigDecimal max, min;
        for(int i=0; i<n-1; i++) {
            for(int j=i+1; j<n; j++) {
                max = (new BigDecimal(s[i])).max(new BigDecimal(s[j]));
                min = (new BigDecimal(s[i])).min(new BigDecimal(s[j]));
                s[i] = max.toString();
                s[j] = min.toString();
                 
                
            }
        }
         

        System.out.println(Arrays.toString(s));

    }

}

我为以下输入运行此程序: *9

-100, 50, 0, 56.6, 90, 0.12, .12, 02.34, 000.000*

输出是: [90, 56.6, 50, 2.34, 0.12, 0.12, 0, 0, -100]

【问题讨论】:

标签: java string sorting bigdecimal


【解决方案1】:

您可以将内部循环体更改为:

String si = s[i], sj = s[j];
if (new BigDecimal(si).compareTo(new BigDecimal(sj)) < 0) {
    s[i] = sj;
    s[j] = si;
}

您不需要将转换后的大小数的结果存储回数组中,而仅用于比较。

对于输入:

9, -100, 50, 0, 56.6, 90, 0.12, .12, 02.34, 000.000

我回来了:

90, 56.6, 50, 9, 02.34, .12, 0.12, 0, 000.000, -100

DEMO

如果您不使用大量的大数字,正常的doubles 就足够了:

if(Double.parseDouble(si) < Double.parseDouble(sj))

【讨论】:

    【解决方案2】:

    试试这样的东西(未经测试):

            List<String> sortedNumberStrings = Arrays.stream(numberStrings)
                    .map(NumberString::new)
                    .sorted(Comparator.reverseOrder())
                    .map(NumberString::getStringValue)
                    .collect(Collectors.toList());
    
            System.out.println(sortedNumberStrings);
    
    
    
    private static class NumberString implements Comparable<NumberString> {
    
            private final String stringValue;
            private final BigDecimal bigDecimalValue;
    
            NumberString(String stringValue) {
    
                this.stringValue = stringValue;
                this.bigDecimalValue = new BigDecimal(stringValue);
            }
    
            public String getStringValue() {
                return stringValue;
            }
    
            public BigDecimal getBigDecimalValue() {
                return bigDecimalValue;
            }
    
            private static final Comparator<NumberString> NATURAL_ORDER_COMPARATOR =
                    Comparator.comparing(NumberString::getBigDecimalValue) // sort by value
                            .thenComparing(NumberString::getStringValue); // sort lexically
    
            @Override
            public int compareTo(NumberString other) {
                return NATURAL_ORDER_COMPARATOR.compare(this, other);
            }
    
            @Override
            public int hashCode() {
                return Objects.hash(stringValue, bigDecimalValue);
            }
    
            @Override
            public boolean equals(Object o) {
                if (this == o) {
                    return true;
                }
                if (!(o instanceof NumberString)) {
                    return false;
                }
                NumberString that = (NumberString) o;
                return Objects.equals(stringValue, that.stringValue) &&
                        Objects.equals(bigDecimalValue, that.bigDecimalValue);
            }
    
            @Override
            public String toString() {
                return stringValue;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2018-06-15
      • 2021-07-03
      • 1970-01-01
      • 2023-03-18
      • 2010-10-15
      • 2015-07-16
      • 2016-09-23
      • 1970-01-01
      • 2020-03-19
      相关资源
      最近更新 更多