Lists and Decimal numbers

A

Ana Dionísio

So, I have this script that puts in a list every minute in 24 hours

hour=[]
i=0
t=-(1.0/60.0)
while i<24*60:
i = i+1
t = t+(1.0/60.0)
hour.append([t])

When it is doing the cicle it can have all the decimal numbers, but I need to print the result with only 4 decimal numbers

How can I define the number of decimal numbers I want to print in this case? For example with 4 decimal numbers, it would print:

0.0000
0.0167
0.0333
....

Can you help?
 
W

Wanderer

So, I have this script that puts in a list every minute in 24 hours



hour=[]

i=0

t=-(1.0/60.0)

while i<24*60:

i = i+1

t = t+(1.0/60.0)

hour.append([t])



When it is doing the cicle it can have all the decimal numbers, but I need to print the result with only 4 decimal numbers



How can I define the number of decimal numbers I want to print in this case? For example with 4 decimal numbers, it would print:



0.0000

0.0167

0.0333

...



Can you help?

You can use round.

for t in hour:
print round(t,4)
 
P

Peter Otten

Ana said:
So, I have this script that puts in a list every minute in 24 hours

hour=[]
i=0
t=-(1.0/60.0)
while i<24*60:
i = i+1
t = t+(1.0/60.0)
hour.append([t])

In many cases you can write

for i in range(...):
...

instead of incrementing manually.
When it is doing the cicle it can have all the decimal numbers, but I need
to print the result with only 4 decimal numbers

How can I define the number of decimal numbers I want to print in this
case? For example with 4 decimal numbers, it would print:

0.0000
0.0167
0.0333
...

Can you help?
.... print "{:.4f}".format(i/60.0)
....
0.0000
0.0167
0.0333
0.0500
[...]
23.9500
23.9667
23.9833
See also

<http://docs.python.org/2/library/string.html#format-specification-mini-language>
 
M

Mark Lawrence

Ana said:
So, I have this script that puts in a list every minute in 24 hours

hour=[]
i=0 t=-(1.0/60.0)
while i<24*60:
i = i+1 t = t+(1.0/60.0)
hour.append([t])

In many cases you can write

for i in range(...):
...

instead of incrementing manually.
When it is doing the cicle it can have all the decimal numbers, but I
need to print the result with only 4 decimal numbers

How can I define the number of decimal numbers I want to print in this
case? For example with 4 decimal numbers, it would print:

0.0000 0.0167 0.0333 ...

Can you help?

for i in range(24*60):
... print "{:.4f}".format(i/60.0)
...
0.0000 0.0167 0.0333 0.0500 [...]
23.9500 23.9667 23.9833See also

<http://docs.python.org/2/library/string.html#format-specification-mini-
language>


and a list comprehension would streamline things further

t=[round(x*1.0/60),4 for x in range(1440)] #compatible with V2.7 & V3.0)

Really?

c:\Users\Mark\Python>python
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32
bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
t=[round(x*1.0/60),4 for x in range(1440)] #compatible with V2.7 &
V3.0)
File "<stdin>", line 1
t=[round(x*1.0/60),4 for x in range(1440)] #compatible with V2.7 &
V3.0)
^
SyntaxError: invalid syntax
 
G

Grant Edwards

and a list comprehension would streamline things further

t=[round(x*1.0/60),4 for x in range(1440)] #compatible with V2.7 & V3.0)

There's a typo in the above. It should be:

t = [round((x*1.0/60),4) for x in range(1440)]
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top