简体   繁体   English

如何截断二维双数组中某些双数的小数位?

[英]How can I cut off decimal places of some double numbers in a 2d double array?

I have this code, which generates me a 10x20 array with a lot of random numbers.我有这个代码,它为我生成一个包含大量随机数的 10x20 数组。 But the values in the first 5 columns should not have any decimal places (see the example below).但是前 5 列中的值不应有任何小数位(请参见下面的示例)。 I don't know, how to fix that.我不知道,如何解决这个问题。

public class scratch {
    public static void main(String[] args) {
        double[][] values = new double[10][20];
            for (int i = 0; i < values.length; i++) {
                for (int j = 0; j < values[i].length; j++) {
                    double x = Math.random();
                    double y = Math.random();
                    if (j == 0) {
                        values[i][j] = Math.random() < 0.5 ? 0 : 1;
                    } else if (j == 1) {
                        values[i][j] = Math.floor(((Math.random() * (250000000 - 500000)) + 500000));
                    } else if (j == 2) {
                        values[i][j] = (Math.random() * (23)) + 1;
                    } else if (j == 3) {
                        values[i][j] = x < 0.25 ? 1 : x < 0.5 ? 3 : x < 0.75 ? 7 : 20; 
                    } else if (j == 4) {
                        values[i][j] = y < 0.25 ? 1 : y < 0.5 ? 3 : y < 0.75 ? 7 : 20;
                    } else {
                        values[i][j] = Math.floor(Math.random() * 100000) / 100000;  
                    }
                    System.out.print(values[i][j] + " ; ");
                }
                System.out.println();
            }
    }
}

example:例子:

0 ; 1688426 ; 15 ; 7 ; 3 ; 0.59743 ; 0.98284 ; ...

1 ; 69715358 ; 17 ; 20 ; 1 ; 0.78631 ; 0.75347 ; ...

you defined a double[][] array,try Number[][] and cast first 5 columns to integer您定义了一个double[][]数组,尝试Number[][]并将前 5 列转换为整数

   public class scratch {
        public static void main(String[] args) {
            Number[][] values = new Number[10][20];
            for (int i = 0; i < values.length; i++) {
                for (int j = 0; j < values[i].length; j++) {
                    double x = Math.random();
                    double y = Math.random();
                    if (j == 0) {
                        values[i][j] = Math.random() < 0.5 ? 0 : 1;
                    } else if (j == 1) {
                        values[i][j] = (int)Math.floor(((Math.random() * (250000000 - 500000)) + 500000));
                    } else if (j == 2) {
                        values[i][j] = (int)(Math.random() * (23)) + 1;
                    } else if (j == 3) {
                        values[i][j] = x < 0.25 ? 1 : x < 0.5 ? 3 : x < 0.75 ? 7 : 20;
                    } else if (j == 4) {
                        values[i][j] = y < 0.25 ? 1 : y < 0.5 ? 3 : y < 0.75 ? 7 : 20;
                    } else {
                        values[i][j] = Math.floor(Math.random() * 100000) / 100000;
                    }
                    System.out.print(values[i][j] + " ; ");
                }
                System.out.println();
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM