Home » excel » java – Show decimal places in percentage and decimal point only if needed

java – Show decimal places in percentage and decimal point only if needed

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m trying to set a Data format in POI so that it will show values like this:

12.5%
12.75% 15%

So, it should only show the decimal point if needed and then only show the fractional values if needed.

I have it partially working using this format:

stylePercentage.setDataFormat(workbook.createDataFormat().getFormat("#.##%"));

However, this prints 15% like:

15.%

From the research I’ve been doing, I’m not sure if what I am trying is possible.

Is there a way to alter the data formatter to only show the decimal point if it is needed?

How to&Answers:

Check out BuiltinFormats.
HSSFDataFormat has a method that can retrieve these for you.

workbook.createDataFormat().getFormat(HSSFDataFormat.getBuiltinFormat("0.00%"));