日期:2014-05-17  浏览次数:20658 次

关于jxl写小数的问题
我需要用jxl来写execle文件,相应的语句为: 
numb = new Number(6, 1, tempPrice);
 sheet.addCell(numb);

其中tempPrice是从数据库中取到的,数据库中的值为2.30。

当写完excel文件后,发现该值在excel文件中变成了2.29999995231628,请问我该如何限定格式啊,让写入的值为2.30呢

多谢了!


------解决方案--------------------
Java code
HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("format sheet");
    HSSFCellStyle style;
    HSSFDataFormat format = wb.createDataFormat();
    HSSFRow row;
    HSSFCell cell;
    short rowNum = 0;
    short colNum = 0;
    row = sheet.createRow(rowNum++);
    cell = row.createCell(colNum);
    cell.setCellValue(11111.25);
    style = wb.createCellStyle();
    style.setDataFormat(format.getFormat("0.0"));
    cell.setCellStyle(style);
    row = sheet.createRow(rowNum++);
    cell = row.createCell(colNum);
    cell.setCellValue(11111.25);
    style = wb.createCellStyle();
    style.setDataFormat(format.getFormat("#,##0.0000"));
    cell.setCellStyle(style);
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();

------解决方案--------------------
数据格式自定义。。。