将数字打印为整数(无小数)

时间:2013-01-31 01:12:36

标签: python python-2.7 python-2.x

任何人都可以帮助我使用Python吗?当我运行以下代码时,L_L_1的输出为1.0,但我希望它只有1。

import math

d_ = 0          # My student number last digit
n_ = 10 + 0     # nth roots of unity

import math as m     # for real
import cmath as m    # for complex

# K_     [ number of the root]
K_ = 9
Root = m.exp((2 * K_ * m.pi * 1j) / n_)      # determine the nth root of unity by using exponential form
Root1 = math.cos((2 * K_ * m.pi / n_)) + 1j * math.sin((2 * K_ * m.pi / n_))   # determine the nth root of unity by using polar form
p_ = (Root) **  (n_)      # 10th power of Root
p_1 = (Root1) **  (n_)    # 10th power of Root1
L_ = (Root.real ** 2 + Root.imag ** 2)       # Test whether the point Root lies on unit circle
L_1 = (Root1.real ** 2 + Root1.imag ** 2)      # Test whether the point Root1 lies on unit circle

print  L_    
print  L_1

1 个答案:

答案 0 :(得分:0)

print int(math.round(L_))

如果您想要更少的舍入,可以使用可选的第二个round参数来指定多个位置。不要完全遗漏,否则0.9999999变为0。

如果您想首先将该值设为整数,请在赋值时执行:

L_ = int(math.round((Root.real ** 2 + Root.imag ** 2), 6)