excel 阅读中额外的小数位
Extra decimal places in excel reading
我一直在为我妈妈写一个小程序,我必须在其中根据他们要求她做的工作显示价格列表。由于她需要价格中的所有小数位,我将 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;
}
class 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。
我找到了解决方案。问题出在我这样做的 LeggiExcel class 中:
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
然后获取值,return 将其作为双精度值。然后,我会将其转换为 LeggiCella
方法中的字符串,并将其转换为 return 字符串。看起来将 Excel 文件中的单元格格式设置为 7 位小数会使它在数字的近似值中变得疯狂,因此它会在末尾设置 8 或 9 个零。我现在已经删除了 NumberCell 检查,并得到了单元格的值 return 作为一个字符串,它工作正常。感谢所有帮助我找到解决方案的人。
我一直在为我妈妈写一个小程序,我必须在其中根据他们要求她做的工作显示价格列表。由于她需要价格中的所有小数位,我将 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;
}
class 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。
我找到了解决方案。问题出在我这样做的 LeggiExcel class 中:
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
然后获取值,return 将其作为双精度值。然后,我会将其转换为 LeggiCella
方法中的字符串,并将其转换为 return 字符串。看起来将 Excel 文件中的单元格格式设置为 7 位小数会使它在数字的近似值中变得疯狂,因此它会在末尾设置 8 或 9 个零。我现在已经删除了 NumberCell 检查,并得到了单元格的值 return 作为一个字符串,它工作正常。感谢所有帮助我找到解决方案的人。