【问题标题】:Extra decimal places in excel reading [duplicate]excel阅读中的额外小数位[重复]
【发布时间】:2015-04-05 06:55:53
【问题描述】:

我一直在为我妈妈编写一个小程序,其中我必须根据他们要求她做的工作显示价格清单。由于她需要价格中的所有小数,我将 Excel 上的单元格格式设置为 7 位小数。问题是调试我的程序时,当我读取一个包含价格的单元格时,它会在实际未写在单元格上的数字之后添加几个零。我不明白发生了什么,我正在使用 OpenOffice 将文件保存为 Excel 97/2003 文件,例如“listino.xls”。这是我用来获取作品标题和价格的代码:

public Lavorazione[] creaLavorazioni(Lavorazione[] lavorazioni){ //create Work

    char[] stringa = new char[8];
    double prezzo = 0;
    String prezzostringa = "";

    for(int righe=1;righe<workbookR.getSheet(0).getRows();righe++){ //until the rows of
//the excel table end

        try{
            lavorazioni[righe-1].setLavorazione(l.LeggiCella(listino, 0, righe));
            stringa = l.LeggiCella(listino, 2, righe).toCharArray();
            for(int i=0;i<stringa.length;i++){ //getting number and switching ","
//with "."
                if(stringa[i]==',')
                    stringa[i]='.';

                prezzostringa += stringa[i]; //Creating the price string
            }

            prezzostringa=prezzostringa.substring(0, 8); // deleting extra zeros, just
//in case
            prezzo = Double.parseDouble(prezzostringa); //casting to double
            lavorazioni[righe-1].setPrezzo(prezzo); //setting in lavorazioni.prezzo

        }catch(Exception e){
            System.out.println("è successo qualcosa in creaLavorazioni!");
        }
        prezzostringa=""; //resetting for the next cycle
    }

    return lavorazioni;
}

Lavorazione 的课程是我做的,是这样的:

public final class Lavorazione {

private String lavorazione; //name of the work to do
private double prezzo; //price of it

public Lavorazione(String lav, double costo){
    this.setLavorazione(lav);
    this.setPrezzo(costo);
}

public String getLavorazione() {
    return lavorazione;
}

public void setLavorazione(String lavorazione) {
    this.lavorazione = lavorazione;
}

public double getPrezzo() {
    return prezzo;
}

public void setPrezzo(double prezzo) {
    this.prezzo = prezzo;
}

所以它只有作品的名称和相对价格。简而言之,当单元格中有“0,05423”时,它会写入“0.05423000000000001”。谁能帮我解决这个问题?使用 java.util.Locale 是个好主意吗?如果您需要更多信息,请询问我。

编辑:我正在使用 jxl api。

【问题讨论】:

  • 你试过用 BigDecimal 代替 double 吗?
  • ""0,05423" 在单元格中,它写入“0.05423000000000001”。” 似乎它需要一个NumberFormat 用于输出。可能相关What Every Computer Scientist Should Know About Floating-Point Arithmetic
  • @bot 我尝试使用 BigDecimal,但它并没有解决问题,只是变得更难处理。
  • @ErwinBolwidt 它很有用,但它并没有告诉我应该如何处理这个问题,因为它只解释了它发生的原因。
  • @AndrewThompson 这真的很长,我会读它,谢谢你的链接。无论如何,一位朋友建议看一下 java.util.Locale 所以我要检查一下。大家怎么看?

标签: java excel swing floating-point numbers


【解决方案1】:

我找到了解决方案。问题出在我这样做的 LeggiExcel 类中:

public String LeggiCella(Sheet foglio, int col, int riga) {

        Cell a = foglio.getCell(col, riga);
        String contenuto = a.getContents();

        if (a.getType() == CellType.NUMBER){ 
            contenuto = Double.toString(LeggiCellaNumerica(a));
            } 

        return contenuto;
    }

    public double LeggiCellaNumerica(Cell a){
        double num = 0;

        NumberCell nc = (NumberCell) a; 
        num = nc.getValue();

        return num;
    }

因此它会检查CellType 是否为NumberCell,然后获取该值,并将其作为双精度值返回。然后,我会将其转换为 LeggiCella 方法中的字符串,并将其作为字符串返回。看起来将 Excel 文件中的单元格格式设置为小数点后 7 位会使数字近似值变得疯狂,因此它会在末尾设置 8 个或 9 个零。我现在已经删除了 NumberCell 检查,只是将单元格的值作为字符串返回,它可以正常工作。感谢所有帮助我找到解决方案的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    相关资源
    最近更新 更多