I've been writing a little program for my mother, in which I have to display a list of prices based on the work they ask her to do. Since she needs all the decimals in the price I set the Cell Format on Excel to have 7 decimals. The problem is that debugging my program, when I go reading a cell with a price in it, it adds several zeros after the number which are not actually written on the cell. I can't understand what's going on and I'm using OpenOffice saving the file as an Excel 97/2003 file, so as "listino.xls". Here's the code I'm using to get the title of the work and the price:
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;
}
the class Lavorazione
is made by me and it's this:
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;
}
So it just has the name of the work and the relative price. In a few words, when there's "0,05423" in the cell, it writes "0.05423000000000001". Can anyone help me about this? Is using the java.util.Locale a good idea? If you need more info, ask me please.
EDIT: I'm using the jxl api.