Print numbers with a percentage sign
Asked Answered
T

1

5

I tried to use one %, and double %%, both seem to multiply my number to 100 times bigger.

This is what it looks like:

percent_format = workbook.add_format({'num_format': '0.00%'})

result: 9403.00%

percent_format = workbook.add_format({'num_format': '0.00%%'})

result: 9403.00%%

percent_format = workbook.add_format({'num_format': '0.00'})

result: 94.03, which is what I want, I just need to append a % on the end.

Any thoughts?

Tantalic answered 25/3, 2015 at 23:51 Comment(4)
If the value is 94.03 then simply formatting it as a percent will (correctly) yield 9403.00%. This is because 1 == 100%.Grivet
@pnuts has it, you need a custom string format, not a number format, to simply append the % to the way the value appears. This will not in any way alter the underlying datta, it's just a mask that changes the way the cell looks, visually.Grivet
Thank you!! pnuts, 0.00"%"' works, you saved my day!Tantalic
haha, I was asked to put a tag, I guess I clicked the wrong one. sorry. let me see if I can change it.Tantalic
S
9

The Format Class has a lot of detail about formatting with XlsxWriter. Around halfway there is a section on format.set_num_format() though this does not specifically mention formatting for percentage sign. It can be achieved so:

percent_format = workbook.add_format({'num_format': '0.00"%"'})  

This merely appends % (say 9 to 9%) without any conversion from say 9 to 900%.

Scat answered 26/3, 2015 at 3:43 Comment(1)
New link to docs: xlsxwriter.readthedocs.io/format.html?highlight=formatAlgesia

© 2022 - 2024 — McMap. All rights reserved.