Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print numbers with a percentage sign

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?

like image 347
Kaiyu Li Avatar asked Mar 25 '15 23:03

Kaiyu Li


People also ask

How do you print a percent sign?

To obtain the character "%" in a format string, enter "%%", e.g. printf("The percentage is %d %%. *n",5); yields The percentage is 5 %. Most users will find that "%g" is the best choice for printing out floating point numbers.

What does %s mean in Python?

The % symbol is used in Python with a large variety of data types and configurations. %s specifically is used to perform concatenation of strings together. It allows us to format a value inside a string. It is used to incorporate another string within a string.

How do you write a percent sign in string format?

You can do this by using %% in the printf statement. For example, you can write printf(“10%%”) to have the output appear as 10% on the screen.


1 Answers

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%.

like image 53
pnuts Avatar answered Oct 17 '22 05:10

pnuts