0

I wrote a very simple code in Python. I need to print all the numbers between 1 and 5 (including 1 and 5) in steps of 0.1. The integers should be without the suffix .0, i.e. the output should be:

0
0.1
0.2
...
1
1.1
...
...
5

Here is my code:

for i in range(0,51):
    if i%10==0:
        z=int(i/10)
        print (z)
    else:
        print (i*0.1)

Here is my output:

0
0.1
0.2
0.30000000000000004
0.4
0.5
0.6000000000000001
0.7000000000000001
0.8
0.9
1
1.1
1.2000000000000002
1.3
1.4000000000000001
1.5
1.6
1.7000000000000002
1.8
1.9000000000000001
2
2.1
2.2
2.3000000000000003
2.4000000000000004
2.5
2.6
2.7
2.8000000000000003
2.9000000000000004
3
3.1
3.2
3.3000000000000003
3.4000000000000004
3.5
3.6
3.7
3.8000000000000003
3.9000000000000004
4
4.1000000000000005
4.2
4.3
4.4
4.5
4.6000000000000005
4.7
4.800000000000001
4.9
5

I don't need all this zeros .000000001.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
Error 404
  • 427
  • 5
  • 25

6 Answers6

2

Taking advantage of Python's string formatting capabilities, you can reduce your whole loop to one line:

for i in range(0, 51):
    print('{:.{deci}f}'.format(i*0.1, deci=min(1, i%10)))

Output:

0
0.1
0.2
...
0.8
0.9
1
1.1
1.2
...
1.8
1.9
2
2.1
2.2
...
4.8
4.9
5

Explanation

This gives you 0 for multiples of 10 for i and 1 for others:

>>> i = 10
>>>  min(1, i%10)
0
>>> i = 8
>>> min(1, i%10)
1

Now, you can use this for the keyword argument deci to determine the number of decimals in your output:

>>> '{:.{deci}f}'.format(1.99, deci=1)
'2.0'
>>> '{:.{deci}f}'.format(1.99, deci=0)
'2'
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
1

You can print the floating point numbers rounded to 1 digit after the decimal point like so:

d = 1.12343214 
"{:.1f}".format(d)
>> 1.1

The second line formats d as a string representation of a float number rounded with 1 digit(comming from the .1) after the decimal point.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
1

You could use round or format

for i in range(0,51):
    if i%10==0:
        z=int(i/10)   
    else:
        z= (i*0.1)
    print '%.1f' % round(z, 1)
    print format(z, '.1f')
Shijo
  • 9,313
  • 3
  • 19
  • 31
1
for i in range(0,51):
    if i%10==0:
        z=int(i/10)
        print (z)
    else:
        print ("%.1f" % (i*0.1))

you can use the %.1f notation. That will limit the number of decimals to be printed

OUTPUT

0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
2
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
3
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9
4
4.1
4.2
4.3
4.4
4.5
4.6
4.7
4.8
4.9
5
Maddy
  • 2,025
  • 5
  • 26
  • 59
1

Change your last line into:

print ( "%.1f" % (i*0.1) )

This will format your numbers showing only one decimal position.

xzoert
  • 308
  • 1
  • 6
1
for i in range(0,51):
    if i%10==0:
        z= round(i/10)
        print (z)
    else:
        i = i * 0.1
        print ("%.1f" % i)

Output:

0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
2
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
3
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9
4
4.1
4.2
4.3
4.4
4.5
4.6
4.7
4.8
4.9
5
Taufiq Rahman
  • 5,600
  • 2
  • 36
  • 44