How to show percentage

Steve Holden steve at holdenweb.com
Fri Sep 23 06:39:33 EDT 2005


Sen-Lung Chen wrote:
> Dear All:
>  I have a question of show percentage.
> For example ,I want to show the percentage of 1/3 = 33.33%
> 
>  I use the 1*100/3 = 33
> it is 33 not 33.33 , how to show the 33.33 %
>  Thanks
> 
You should by now know enough answers. The easiest way to ensure that 
the result is floating point is to cast it as

   pct = 100.0 * v) / N

This is guaranteed to work in all past and future Python versions 
without setting any options. Then you can format is using the % operator.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                          www.pycon.org




More information about the Python-list mailing list