我的问题涉及在人类可读的表单中格式化浮点数。
例如在下面的例子中,结果是一个浮点数,我想我在截断发生的位置上犯了一个错误。你会发现它就在最后一行代码中。
天文学家倾向于使用0.12345地球重量或木星重量,具体取决于行星的类别。
感谢您对此进行调查,感谢您的光临。
#Planet mass relator
#A command-line program written by Anis Ali Khan
#Customary Greeting
greeting_string = "Have you found an Earth-like planet?\n\n This program lets your enter the weight in Kg\n to know it in terms we can relate to.\n"
hint_string = "For example, the weight of the Earth's moon, Luna is 7.35e22\n"
print(greeting_string)
print(hint_string)
#Earth Mass
earth_mass = 5.9742e24 #in Kg
#Jupiter Mass not yet implemented
#Jupiter Mass
#jupiter_mass = 1.898e27 #in Kg
prompt_text = 'Please enter the mass of the planet that was discovered in Kg: '
found_planet_mass = float(input(prompt_text))
ratio_decimal = 0
#ratio_fraction = []
found_relative_to_earth = float(found_planet_mass / earth_mass)
results_text_Earth = 'The planet you found weighs {m1} times the weight of the Earth\n'.format(m1=found_relative_to_earth)
print(results_text_Earth.format('.5g')) #give 5 significant digits
答案 0 :(得分:0)
您应该在第一个格式声明中使用格式标识符(5g
)。 print
语句中的格式不执行任何操作:
results_text_Earth = 'The planet you found weighs {m1:5g} times the weight of the Earth\n'.format(m1=found_relative_to_earth)
print(results_text_Earth)
答案 1 :(得分:0)
我认为这对你有用:
{{1}}