Firstly, an observation. Do not create the cell styles like that; within the
for loop I mean. If you do this, you will be creating multiple cell styles -
possibly - all with the same name and properties. Ideally, you should create
the style outside of the for loop and then apply it whenever you encounter a
numeric cell. The main reason for this is performance; you do not want your
code to be re-creating identical objects again and again. So, write
something like this;
HSSFCellStyle cs = wb.createCellStyle();
HSSFDataFormat df = wb.createDataFormat();
cs.setDataFormat(df.getFormat("0.0"));
cs.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
for (int i=14;i<rows;i++)
{
HSSFCell y= wb.getSheetAt(0).getRow(i).getCell((short)
colValue);
if (y.getCellType() == HSSFCell.CELL_TYPE_STRING) {
} else if (y.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
float d=(float) y.getNumericCellValue();
y.setCellStyle(cs);
}
}
Now, another question I am afraid and forgive me if this sounds pedantic. In
your question, you talk of 'rounding' the numeric value and can I ask again
what is it that you expected to see happen? This morning, I have just
checked in Excel and found that if I set the format of the cell to "0.0" and
entered, for example 1.25, then this value would be rounded up to 1.3.
Indeed, this same behaviour will be displayed for any value between 1.25 to
1.29, all will be rounded up to 1.3. What Excel did not do however was to
round the value down; if I entered any value between 1.24 and 1.21, Excel
displayed 1.2 in the cell. Is this the behaviour you are seeing and if not,
can I please ask again, what are you seeing when you open the file?
Yours
Mark B
PS. This behaviour holds true as the number of digits following the decimal
point increases. By this, I mean that 123.159 would be displayed as 123.1
even though you might expect Excel to display 1.2.
--
View this message in context:
http://apache-poi.1045710.n5.nabble.com/Round-to-one-Decimal-place-not-happenning-using-style-setDataFormat-format-getFormat-0-0-tp3404980p3406229.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]