poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Chamarthy, Ravi Kiran \(Exchange\)" <rchamar...@Bear.com>
Subject RE: Problem when setting a double value to a cell
Date Thu, 21 Apr 2005 16:52:11 GMT
Thanks Frans and Amol for your valuable suggestions.

Resolved the problem. I changed the code as :

   int tempvar = (int) (actualdoublevar*100);

      c.setCellValue((double) tempvar/100);

The only problem with this code is if the precision needs to be changed to 3 digits then the
code needs to be touched. But anyways the client is very happy.

Thanks a lot,
Ravi


-----Original Message-----
From: Amol Deshmukh [mailto:adeshmukh@hobsons-us.com]
Sent: Thursday, April 21, 2005 12:36 PM
To: 'POI Users List'
Subject: RE: Problem when setting a double value to a cell


Ravi,

Can you post the code that initializes the double variable?
My guess is that you are upcasting a float to a double that 
is the root cause of this loss of precision.

In any case, Frans' suggestion is a good one if possible to
implement in your scenario. In my excel, i retain the cell values
as the formulae which allows users to see the calculations 
in excel as well as avoids issues like this one.

~ amol

> -----Original Message-----
> From: Frans.Flippo@INGBank.com [mailto:Frans.Flippo@INGBank.com]
> Sent: Thursday, April 21, 2005 11:38 AM
> To: poi-user@jakarta.apache.org
> Subject: RE: Problem when setting a double value to a cell
> 
> 
> 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.
> -----------------------------------------------------------------
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/



**********************************************************************
Please be aware that, notwithstanding the fact that the person sending
this communication has an address in Bear Stearns' e-mail system, this
person is not an employee, agent or representative of Bear Stearns.
Accordingly, this person has no power or authority to represent, make
any recommendation, solicitation, offer or statements or disclose
information on behalf of or in any way bind Bear Stearns or any of its
affiliates.
**********************************************************************


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


Mime
View raw message