Skip to content

set percentage as cell format #2616

Answered by MarkBaker
saginadav asked this question in Q&A
Discussion options

You must be logged in to vote

Welcome to the way MS Excel works with percentages.

4% is 0.04 (4 parts out of 100); 0.04% is 0.0004 (0.04 parts out of 100)

So divide the value by 100, and set the format code as '0.00%

$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" NumberFormat::FORMAT_PERCENTAGE_00 format code for percentage with 2-decimal places

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by saginadav
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants