easy float question just eludes me

Steve Holden steve at holdenweb.com
Thu Jul 28 10:08:03 EDT 2005


nephish at xit.net wrote:
> Hullo all !
> 
> i have a real easy one here that isn't in my book.
> i have a int number that i want to divide by 100 and display to two
> decimal places.
> 
> like this float(int(Var)/100)
> but i need it to display the .00 even if it does not have a .00 value
> like this
> if Var is 50, i need to display .50 instead of just .5
> 
> i know there is an easy way to do this.
> thanks
> 

Play with this:

  >>> "%.2f"%0.5
'0.50'
  >>> a = 50.0
  >>> ("%.2f" % (a/100))[-3:]
'.50'
  >>>

I presume that a stays in the range 0 <= a < 100.
If not you will have to handle the integral digits as well with 
something like

  >>> "%4.2f" % 3.1
'3.10'
  >>> "%6.2f" % 3.1
'  3.10'
  >>>

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC             http://www.holdenweb.com/




More information about the Python-list mailing list