我正在制作一个数学求解程序,它会将整数打印为小数。例如1会被打印为1.0,5会被打印为5.0,我的代码如下:
print("Type in the cooridinates of the two points.")
print("")
print("---------------------")
print("First point:")
x1 = int(input("X: "))
y1 = int(input("Y: "))
print("")
print("---------------------")
print("Second point:")
x2 = int(input("X: "))
y2 = int(input("Y: "))
m = (y1-y2) / (x1-x2)
b = y1 - m * x1
round(m, 0)
round(b, 0)
print("Completed equation:")
print("")
if b < 0:
print("Y = "+ str(m) +"X - "+ str(b) +".")
elif b > 0:
print("Y = "+ str(m) +"X + "+ str(b) +".")
elif b == 0:
print("Y = "+ str(m) +"X.")
input("Press enter to continue.")
'{:g}'
和'{:.0g}'
之间的区别,因为我认为后者可能是他实际想要的。(值得指出的是,他已经试图将数字四舍五入到最近的整数,但是没有成功。) - abarnert