You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Rob Y <ry...@yahoo.com> on 2008/11/25 00:06:11 UTC
number of decimal places in data formats
I'm writing code to dynamically assign an appropriate data type and format to
data presented as strings. That is, I check the string for dollar,
percentage, numeric or date data before defaulting to string data.
All works fine (though it's a pain to have to do all that - it'd be nice to
have a method somewhere in POI to analyze a string and produce a data type,
format and optional numeric value in some highly optimized way).
Anyway, my problem comes with decimals. I'm able to use getFormat to
produce custom formats with a variable number of decimal places based on how
many were in the input string, and that works fine. Excel seems to display
these on the 'format cell' dialog under Custom. But it also displays an
explicit 'Decimal places' edit box where you can control this.
My question. Does excel support data formats like "0.%" with the number of
decimal places specified explicitly somewhere other than in the format
string? It would seem much more efficient to work that way rather than to
have to parse format strings to determine the number of decimal places.
I guess this is another stupid question in the sense of 'why do you think we
call it the Horrible Spreadsheet Format'? But can't hurt to ask.
In the meantime, I'm also wondering whether Java-wise, it'd be more
efficient to keep my own list of data type, #decimals combinations I've
encountered so far scan that for formats. Or is it okay to just dynamically
build a string with the right number of decimals each time and let getFormat
take care of searching its list. In other words, are all those string
concatenations expensive processing wise? Or does it all pale next to the
parsing of the original strings to determine #decimals to begin with...
Thanks,
Rob
--
View this message in context: http://www.nabble.com/number-of-decimal-places-in-data-formats-tp20671940p20671940.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org
Re: number of decimal places in data formats
Posted by Rob Y <ry...@yahoo.com>.
Anthony Andrews wrote:
>
> Hello Rob
>
> If I remember correctly, getFormat() will take care of part of the problem
> for you - it will check to see if a format exists with those features and,
> if not, add it to the collection for the workbook.
>
> As for the first part of your question, I cannot offer an answer. I would
> assume however that there is no way to separate the two pieces of
> information - that is to specify that the number have a decimal portion in
> the format and the number of places separately. Of course, you could do
> something similar in your own application by creating a format class that
> would contain all of the formatting information to be parsed just before
> you called the write() method. May even look at it myself someday.
>
>
Yes, you're right that getFormat() will use an existing format if there's a
match. I was just worried about the efficiency of creating those format
strings dynamically for each numeric cell. But it seems to work well
enough.
I was about to do something like your suggestion of storing format
parameters in a format class that I could scan instead of producing a format
string for getFormat to look up. Theoretically that would be more
efficient, but I don't know that it would be a noticeable boost to
performance. I did something similar for cell styles, though.
HSSFCellStyle doesn't have something like getFormat that will find or create
a style as needed, so I had to do it. But the approach ended up working
very well.
Rob
--
View this message in context: http://www.nabble.com/number-of-decimal-places-in-data-formats-tp20671940p20682608.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org
Re: number of decimal places in data formats
Posted by Anthony Andrews <py...@yahoo.com>.
Hello Rob
If I remember correctly, getFormat() will take care of part of the problem for you - it will check to see if a format exists with those features and, if not, add it to the collection for the workbook.
As for the first part of your question, I cannot offer an answer. I would assume however that there is no way to separate the two pieces of information - that is to specify that the number have a decimal portion in the format and the number of places separately. Of course, you could do something similar in your own application by creating a format class that would contain all of the formatting information to be parsed just before you called the write() method. May even look at it myself someday.
--- On Mon, 11/24/08, Rob Y <ry...@yahoo.com> wrote:
From: Rob Y <ry...@yahoo.com>
Subject: number of decimal places in data formats
To: user@poi.apache.org
Date: Monday, November 24, 2008, 3:06 PM
I'm writing code to dynamically assign an appropriate data type and format
to
data presented as strings. That is, I check the string for dollar,
percentage, numeric or date data before defaulting to string data.
All works fine (though it's a pain to have to do all that - it'd be
nice to
have a method somewhere in POI to analyze a string and produce a data type,
format and optional numeric value in some highly optimized way).
Anyway, my problem comes with decimals. I'm able to use getFormat to
produce custom formats with a variable number of decimal places based on how
many were in the input string, and that works fine. Excel seems to display
these on the 'format cell' dialog under Custom. But it also displays
an
explicit 'Decimal places' edit box where you can control this.
My question. Does excel support data formats like "0.%" with the
number of
decimal places specified explicitly somewhere other than in the format
string? It would seem much more efficient to work that way rather than to
have to parse format strings to determine the number of decimal places.
I guess this is another stupid question in the sense of 'why do you think
we
call it the Horrible Spreadsheet Format'? But can't hurt to ask.
In the meantime, I'm also wondering whether Java-wise, it'd be more
efficient to keep my own list of data type, #decimals combinations I've
encountered so far scan that for formats. Or is it okay to just dynamically
build a string with the right number of decimals each time and let getFormat
take care of searching its list. In other words, are all those string
concatenations expensive processing wise? Or does it all pale next to the
parsing of the original strings to determine #decimals to begin with...
Thanks,
Rob
--
View this message in context:
http://www.nabble.com/number-of-decimal-places-in-data-formats-tp20671940p20671940.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org