0

I am new to python and am having some difficulties. How would I make this so that I can round the float(roll)*100 rounded to 2 decimal places.

for x in roll1:
        print('%d - %d %f%%' %(x,roll1[x], float(roll1[x])/float(roll)*100))

This is the output currently. I need to make the numbers rounded to 2 decimal places. Thanks!

1 - 1794 17.940000%
2 - 1620 16.200000%
3 - 1620 16.200000%
4 - 1611 16.110000%
5 - 1650 16.500000%
6 - 1705 17.050000%
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
Kay
  • 3
  • 1
  • @Kay, I would recommend indenting code blocks by 4 spaces and surrounding inline code with `. See [formatting help](http://stackoverflow.com/editing-help). – Jed Fox Oct 04 '15 at 20:44

1 Answers1

1

Try %.2f instead of %f and %.

The %f format specifier has these options:

  • A number not preceded by . specifies the maximum length of the output (number + . + decimals)
  • A . followed by a number specifies the number of decimal places.
  • Certain characters immediately after the % have special meanings:
    • (space) means that positive numbers should have a space prepended to them.
    • + means that positive numbers should have a + sign before them.
Jed Fox
  • 2,979
  • 5
  • 28
  • 38