easy float question just eludes me

N

nephish

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
 
S

Simon Brunning

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
Decimal("2.50")
 
S

Sergei Organov

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.
0.50
 
S

Steve Holden

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

regards
Steve
 
T

Terry Reedy

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)

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.
' 0.50'

Terry J. Reedy
 
D

Dennis Lee Bieber

like this float(int(Var)/100)

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:
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
Display is formatting, separate from computational usage...
'0.50'

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top