-
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:
the problem is that the cell displayed 4.% instead of 0.04%. if I remove the line
in the xls displayed 0.04 as number, but I need it to display in the xls 0.04% as number |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Welcome to the way MS Excel works with percentages.
So divide the value by 100, and set the format code as $sheet->setCellValueExplicit($cell, $innervalue / 100, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NUMERIC);
$sheet->getStyle($cell)->getNumberFormat()->setFormatCode('0.00%'); You can also use the "built-in" |
Beta Was this translation helpful? Give feedback.
-
thanks this what works for me:
|
Beta Was this translation helpful? Give feedback.
Welcome to the way MS Excel works with percentages.
4%
is0.04
(4 parts out of 100);0.04%
is0.0004
(0.04 parts out of 100)So divide the value by 100, and set the format code as
'0.00%
You can also use the "built-in"
NumberFormat::FORMAT_PERCENTAGE_00
format code for percentage with 2-decimal places