poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <Frans.Fli...@INGBank.com>
Subject RE: Problem when setting a double value to a cell
Date Thu, 21 Apr 2005 15:37:49 GMT
Hi,
 
Well, apparently your variable contains the value 35.590000653.
 
You don't say what its type is and how it got its value. If you got it
through some arithmetic operation that uses floats, consider using doubles.
Or use one of the rounding functions in java.math (round, ceil, or its
relatives).
 
Even then, remember that not all numbers can be represented by doubles. This
inherent to the internal representation used (IEEE 754 floating point).
Perhaps 35.59 is one of them. This has little to do with either POI or
Excel.
 
If you know you want two decimals precision, you might multiply your number
by 100, round it to an integer, and use a formula in Excel that divides it
by 100 again, e.g.
 
int intvar = variable / 100;
 
c.setCellValue("=" + intvar + " / 100");
 
(Or something like that. I've never used formulas from POI, but I'm sure can
find how to do it in the manual).
 
 
Good luck,
Frans

-----Original Message-----
From: Chamarthy, Ravi Kiran (Exchange) [mailto:rchamarthy@Bear.com]
Sent: Thursday, April 21, 2005 5:27 PM
To: POI Users List
Cc: poi-user-info@jakarta.apache.org
Subject: Problem when setting a double value to a cell


Hi,
 
Can you please help me.
 
My code is written as :
 
        HSSFCellStyle styleDataCommAmt = wb.createCellStyle();
        styleDataCommAmt.setFont(dataFont);
        styleDataCommAmt.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
        styleDataCommAmt.setDataFormat(format.getFormat("$#,##0.00"));
 
        c = r.createCell(cellNum++);
        c.setCellStyle(styleDataCommAmt);
        c.setCellValue(variable);//where variable is of double  type , when
printed shows 35.59
 
 
In the generated Excel sheet, the cell shows as 35.59 but when we click on
the cell the value is in Excel address bar shows as  "35.590000653".
 
Can you please help me how to solve this. I want the cell type to be number
only ( else i can do the formatting using NumberFormat Class and assign the
cell value as string ).
 
Any questions please do let me know.
 
Thanks,

Ravi Chamarthy


 



-----------------------------------------------------------------
ATTENTION:
The information in this electronic mail message is private and
confidential, and only intended for the addressee. Should you
receive this message by mistake, you are hereby notified that
any disclosure, reproduction, distribution or use of this
message is strictly prohibited. Please inform the sender by
reply transmission and delete the message without copying or
opening it.

Messages and attachments are scanned for all viruses known.
If this message contains password-protected attachments, the
files have NOT been scanned for viruses by the ING mail domain.
Always scan attachments before opening them.
-----------------------------------------------------------------


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message