Connecting Tech Pros Worldwide Forums | Help | Site Map

easy float question just eludes me

nephish@xit.net
Guest
 
Posts: n/a
#1: Jul 28 '05
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


Simon Brunning
Guest
 
Posts: n/a
#2: Jul 28 '05

re: easy float question just eludes me


On 28 Jul 2005 06:39:32 -0700, nephish@xit.net <nephish@xit.net> wrote:[color=blue]
> 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[/color]
[color=blue][color=green][color=darkred]
>>> import decimal as dec
>>> dec.Decimal('250.00')/100[/color][/color][/color]
Decimal("2.50")

--
Cheers,
Simon B,
simon@brunningonline.net,
http://www.brunningonline.net/simon/blog/
nephish
Guest
 
Posts: n/a
#3: Jul 28 '05

re: easy float question just eludes me


way cool, outta work out just fine

On 07/28/2005 08:48:27 AM, Simon Brunning wrote:[color=blue]
> On 28 Jul 2005 06:39:32 -0700, nephish@xit.net <nephish@xit.net>
> wrote:[color=green]
> > 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[/color]
> value[color=green]
> > like this
> > if Var is 50, i need to display .50 instead of just .5[/color]
>[color=green][color=darkred]
> >>> import decimal as dec
> >>> dec.Decimal('250.00')/100[/color][/color]
> Decimal("2.50")
>
> --
> Cheers,
> Simon B,
> simon@brunningonline.net,
> http://www.brunningonline.net/simon/blog/
>
>[/color]

Sergei Organov
Guest
 
Posts: n/a
#4: Jul 28 '05

re: easy float question just eludes me


nephish@xit.net writes:
[color=blue]
> 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.[/color]
[color=blue][color=green][color=darkred]
>>> print "%.2f" % (50./100)[/color][/color][/color]
0.50

nephish
Guest
 
Posts: n/a
#5: Jul 28 '05

re: easy float question just eludes me


oops, i dont seem to have a module decimal
-shawn

On 07/28/2005 08:48:27 AM, Simon Brunning wrote:[color=blue]
> On 28 Jul 2005 06:39:32 -0700, nephish@xit.net <nephish@xit.net>
> wrote:[color=green]
> > 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[/color]
> value[color=green]
> > like this
> > if Var is 50, i need to display .50 instead of just .5[/color]
>[color=green][color=darkred]
> >>> import decimal as dec
> >>> dec.Decimal('250.00')/100[/color][/color]
> Decimal("2.50")
>
> --
> Cheers,
> Simon B,
> simon@brunningonline.net,
> http://www.brunningonline.net/simon/blog/
>
>[/color]

Steve Holden
Guest
 
Posts: n/a
#6: Jul 28 '05

re: easy float question just eludes me


nephish@xit.net wrote:[color=blue]
> 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
>[/color]

Play with this:
[color=blue][color=green][color=darkred]
>>> "%.2f"%0.5[/color][/color][/color]
'0.50'[color=blue][color=green][color=darkred]
>>> a = 50.0
>>> ("%.2f" % (a/100))[-3:][/color][/color][/color]
'.50'[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]

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
[color=blue][color=green][color=darkred]
>>> "%4.2f" % 3.1[/color][/color][/color]
'3.10'[color=blue][color=green][color=darkred]
>>> "%6.2f" % 3.1[/color][/color][/color]
' 3.10'[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]

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

Terry Reedy
Guest
 
Posts: n/a
#7: Jul 28 '05

re: easy float question just eludes me



<nephish@xit.net> wrote in message
news:1122557972.833617.157360@o13g2000cwo.googlegr oups.com...[color=blue]
> 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)[/color]

At present, int/int truncates, so this won't do what you want. Use
int/100.0 to get float you want. For output, use % formating operator with
f specification.
[color=blue][color=green][color=darkred]
>>> '%7.2f' % (50/100.0)[/color][/color][/color]
' 0.50'

Terry J. Reedy



Dennis Lee Bieber
Guest
 
Posts: n/a
#8: Jul 28 '05

re: easy float question just eludes me


On 28 Jul 2005 06:39:32 -0700, nephish@xit.net declaimed the following
in comp.lang.python:
[color=blue]
>
> like this float(int(Var)/100)[/color]

First problem -- you are (or were, the operators have been
changing in Python) doing an integer/integer divide which (used to)
returns an integer, and integers don't have fractional parts; and /then/
you are telling it to convert this integer result to a float.

You need to convert the division to floats before performing it:
[color=blue][color=green][color=darkred]
>>> i = 50
>>> i/100 # integer/integer[/color][/color][/color]
0[color=blue][color=green][color=darkred]
>>> float(i)/100 # convert to float/integer[/color][/color][/color]
0.5[color=blue][color=green][color=darkred]
>>> i/100.0 # integer/float[/color][/color][/color]
0.5[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
[color=blue]
> 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
>[/color]
Display is formatting, separate from computational usage...
[color=blue][color=green][color=darkred]
>>> "%7.2f" % (i/100.0)[/color][/color][/color]
' 0.50'[color=blue][color=green][color=darkred]
>>> "%.2f" % (float(i)/100)[/color][/color][/color]
'0.50'

--[color=blue]
> ================================================== ============ <
> wlfraed@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
> wulfraed@dm.net | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <[/color]
Closed Thread