摆脱python中的零

时间:2017-01-13 14:18:28

标签: python output

我在Python中编写了一个非常简单的代码。 我需要以1的步长打印51(包括50.1)之间的所有数字。整数应该没有后缀.0,即输出应该是:

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

这是我的代码:

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

这是我的输出:

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

我不需要所有这些零.000000001

6 个答案:

答案 0 :(得分:2)

利用Python的字符串格式化功能,您可以将整个循环减少到一行:

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

输出:

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

解释

这为0提供了i倍数,其他人为1>>> i = 10 >>> min(1, i%10) 0 >>> i = 8 >>> min(1, i%10) 1 的倍数:

deci

现在,您可以将此用作关键字参数>>> '{:.{deci}f}'.format(1.99, deci=1) '2.0' >>> '{:.{deci}f}'.format(1.99, deci=0) '2' 来确定输出中的小数位数:

public class ApplicationUser : IdentityUser {

} 

public class Server : ApplicationUser {

}

public class HumanUser : ApplicationUser {

    [Required]
    public virtual GlobalInventory GlobalInventory { get; set; }
}

public class GlobalInventory {

    [Key, ForeignKey("User")]
    public string UserId { get; set; }

    [Required]
    public virtual HumanUser User { get; set; }
}

答案 1 :(得分:1)

您可以打印小数点后四舍五入到1位的浮点数,如下所示:

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

第二行将d格式化为一个浮点数的字符串表示形式,该数字在小数点后用1位数(从.1得出)舍入。

答案 2 :(得分:1)

您可以使用圆形或格式

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')

答案 3 :(得分:1)

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

您可以使用%.1f表示法。这将限制要打印的小数位数

输出

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

答案 4 :(得分:1)

将您的最后一行更改为:

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

这将格式化您的数字,只显示一个小数位。

答案 5 :(得分:1)

class WebResponse():
    STATUS_OK = 200
    STATUS_CREATED = 201
    STATUS_ACCEPTED = 202
    STATUS_NOCONTENT = 204
    STATUS_BADREQUEST = 400
    STATUS_UNAUTHORIZED = 401
    STATUS_FORBIDDEN = 403
    STATUS_NOTFOUND = 404
    STATUS_CONFLICT = 409
    STATUS_INVALID = 422
    STATUS_INTERNALSERVER_ERROR = 500

    def __init__(self, r, result=None):
        self.status_code = r.status_code
        self.text = r.text
        self.headers = r.headers
        self.result = r.text if result is None else result

    def __repr__(self):
        return 'WebResponse(%s, %s)' % (self.status_code, self.result)

<强>输出:

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