有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么我在使用ApachePOI的程序中得到一个圆点(句点符号)来引导每个字符串/值?

我第一次使用ApachePOI从excel文件读取和存储数据here's an example of what is happening when I run the program

名字前面不应该有点。 此外,对于某些州,由于excel文件中的值非常大,因此显示:

The data from year 2010 to 2017 for .New York is: [1.9405185E7, 1.9526372E7, 
1.9625409E7, 1.9712514E7, 1.977358E7, 1.9819347E7, 1.9836286E7, 1.9849399E7]

我能用BigDecimal解决这个问题吗?如果是这样的话,我真的不知道怎么做。我的程序代码是

public class PopulationEval {

    public static void main(String[] args) throws IOException, 
         InvalidFormatException 
    { 
        File excelFile = new 
        File("C:\\Users\\gg\\Documents\\2017census.xlsx");
        FileInputStream fis = new FileInputStream(excelFile);

        XSSFWorkbook myWorkbook = new XSSFWorkbook(excelFile);

        XSSFSheet mySheet = myWorkbook.getSheetAt(0);

        Iterator<Row> rowIterator = mySheet.iterator();

        int selectedRow = 1;
        HashMap GeographicArea = new HashMap();
        while (rowIterator.hasNext())
        {
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            int selectedColumn = 1;

            ArrayList yearlyData = new ArrayList();

            String geographicName = "";
            while(cellIterator.hasNext())
            {
                Cell cell = cellIterator.next();
                switch(cell.getCellType())
                {
                    case Cell.CELL_TYPE_NUMERIC:
                        double num = cell.getNumericCellValue();
                        if(selectedColumn !=1 && selectedRow >=3)
                        {
                            yearlyData.add(num);
                        }

                        break;

                    case Cell.CELL_TYPE_STRING:
                        String stringValue = cell.getStringCellValue();
                        //System.out.print(stringValue );
                        if(selectedRow >= 3 && selectedColumn == 1)
                        {
                            geographicName = stringValue;
                        }
                        if(selectedColumn != 1 && selectedRow >= 3)
                        {
                            yearlyData.add(stringValue);
                        }
                        break;

                }
                selectedColumn++; 
            }
            if(!geographicName.isEmpty())
            {
                GeographicArea.put(geographicName, yearlyData);
            }
            //System.out.println();
            selectedRow++;
        }


        fis.close();

        Scanner input = new Scanner(System.in);


        System.out.println("Which state would you like to view data for from 
        2010 to 2017? Use proper spelling.");
        String user = input.nextLine();

            if(GeographicArea.containsKey(user))
            {
                System.out.println("The data from year 2010 to 2017 for " + 
                user + " is: " + GeographicArea.get(user));
            }
        }
} 

共 (1) 个答案

  1. # 1 楼答案

    您可以在任何时候使用此实用程序方法来舍入双精度:

    public static double round(double value, int places) {
            if (places < 0) throw new IllegalArgumentException();
    
            BigDecimal bd = new BigDecimal(Double.toString(value));
            bd = bd.setScale(places, RoundingMode.HALF_UP);
            return bd.doubleValue();
    }
    

    在您的程序中,当您将double值添加到列表中时,请将其包装在此方法调用中,并提供“places”,如下所示:

    yearlyData.add(round(num,5));