How to set cell as percentage in xls - phpoffice

Viewed 53

Hey all I want to set the format of a cell in xls to percentage. I am using phpoffice. the value that I want is 0.04% as percentage. the problem the origin $innervalue is 0.04% as string. this is my code:

if($column == 'E') 
                        {
                            $innervalue = str_replace('%','',$innervalue); //remove the % sign for float
                            $innervalue = (float)$innervalue;              // set innervalue as float and not string
                            $sheet->setCellValueExplicit($cell, $innervalue, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NUMERIC); //put the inner value as number
                            $sheet->getStyle($cell)->getNumberFormat()->setFormatCode('#.##%'); // format the cell for 0.04%
                        }

the problem is that the cell displayed 4.% instead of 0.04%.

if I remove the line

$sheet->getStyle($cell)->getNumberFormat()->setFormatCode('#.##%'); // format the cell for 0.04%

in the xls displayed 0.04 as number, but I need it to display in the xls 0.04% as number

1 Answers
Related