Python 如何显示数字的单位?

Python 如何显示数字的单位?,python,python-3.x,Python,Python 3.x,我写了一个代码,将笛卡尔坐标转换成椭球坐标,它可以工作,但有些变量是度数,有些是米。我必须表现出不同,但我不知道怎么做 def ell2xyz(latitude,longitude,ellHeight): import math a=6378137.0000 b=6356752.3141 e=(math.sqrt(((a**2)-(b**2))/(a**2))) N=a/math.sqrt((1-((e**2)*(math.sin(latitude)**2

我写了一个代码,将笛卡尔坐标转换成椭球坐标,它可以工作,但有些变量是度数,有些是米。我必须表现出不同,但我不知道怎么做

def ell2xyz(latitude,longitude,ellHeight):
    import math
    a=6378137.0000
    b=6356752.3141
    e=(math.sqrt(((a**2)-(b**2))/(a**2)))
    N=a/math.sqrt((1-((e**2)*(math.sin(latitude)**2))))
    x=(float((N+ellHeight)*math.cos(latitude)*math.cos(longitude)))
    y=(float(N+ellHeight)*math.cos(latitude)*math.sin(longitude))
    z=(float((1-e**2)*(N+ellHeight))*math.sin(latitude))
    return x,y,z

latitude,longitude,ellHeight=ell2xyz(41.00000000,29.00000000,500.0000)
print(latitude,longitude,ellHeight)
我建议你用这个方法

我建议你用这个方法


问题是所有变量(纬度、经度和高度)都是浮点数,它们只是数字。单元是对我们人类有意义的抽象,但您的代码无法知道您希望使用的单元是什么。我相信你只是想在屏幕上显示单位。为了做到这一点,你必须手动告诉什么是什么

以下任何一项的工作原理相同:

print(latitude, "degrees,", longitude, "degrees,", ellHeight, "meters")
print("{} degrees, {} degrees, {} meters".format(latitude, longitude, ellHeight))
print(str(latitude) + " degrees, " + str(longitude) + " degrees, " + str(ellHeight) + " meters")
结果将是:

4711572.482946889度,4179837.81128292度,-1005107.7207451101米


问题是所有变量(纬度、经度和高度)都是浮点数,它们只是数字。单元是对我们人类有意义的抽象,但您的代码无法知道您希望使用的单元是什么。我相信你只是想在屏幕上显示单位。为了做到这一点,你必须手动告诉什么是什么

以下任何一项的工作原理相同:

print(latitude, "degrees,", longitude, "degrees,", ellHeight, "meters")
print("{} degrees, {} degrees, {} meters".format(latitude, longitude, ellHeight))
print(str(latitude) + " degrees, " + str(longitude) + " degrees, " + str(ellHeight) + " meters")
结果将是:

4711572.482946889度,4179837.81128292度,-1005107.7207451101米


您可以使用列表,因此您将同时具有浮点和字符串:

def ell2xyz(latitude,longitude,ellHeight):
    import math
    a=6378137.0000
    b=6356752.3141
    e=(math.sqrt(((a**2)-(b**2))/(a**2)))
    N=a/math.sqrt((1-((e**2)*(math.sin(latitude)**2))))
    x=(float((N+ellHeight)*math.cos(latitude)*math.cos(longitude)))
    y=(float(N+ellHeight)*math.cos(latitude)*math.sin(longitude))
    z=(float((1-e**2)*(N+ellHeight))*math.sin(latitude))
    if x > 0:
        x = [x,"N"]
    else:
        x = [x,"S"]
    if y > 0:
        y = [y,"E"]
    else:
        y= [y,"W"]
    z = [z, "m"]
    return x,y,z


latitude,longitude,ellHeight=ell2xyz(41.00000000,29.00000000,500.0000)
print(latitude,longitude,ellHeight)
结果:
[4711572.4829468895,'N'][4179837.81128292,'E'][-1005107.7207451101,'m']
您可以使用列表,因此您将同时具有浮点和字符串:

def ell2xyz(latitude,longitude,ellHeight):
    import math
    a=6378137.0000
    b=6356752.3141
    e=(math.sqrt(((a**2)-(b**2))/(a**2)))
    N=a/math.sqrt((1-((e**2)*(math.sin(latitude)**2))))
    x=(float((N+ellHeight)*math.cos(latitude)*math.cos(longitude)))
    y=(float(N+ellHeight)*math.cos(latitude)*math.sin(longitude))
    z=(float((1-e**2)*(N+ellHeight))*math.sin(latitude))
    if x > 0:
        x = [x,"N"]
    else:
        x = [x,"S"]
    if y > 0:
        y = [y,"E"]
    else:
        y= [y,"W"]
    z = [z, "m"]
    return x,y,z


latitude,longitude,ellHeight=ell2xyz(41.00000000,29.00000000,500.0000)
print(latitude,longitude,ellHeight)
结果:
[4711572.4829468895,'N'][4179837.81128292,'E'][-1005107.7207451101,'m']

不太清楚您在问什么,在python中,浮点型没有单位,您必须手动
打印每个数值后面所需的文本标签。Python不会自动为您这样做。不太确定您的要求是什么,在Python中,
float
没有单元您必须手动
打印每个数值后所需的文本标签。Python不会自动为您这样做。