将输入值转换为十进制(Python 3)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19557498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:03:00  来源:igfitidea点击:

Making an input value into a decimal (Python 3)

pythonpython-3.xdecimal

提问by nerd4life

Currently I have this:

目前我有这个:

gpa = input("Enter your gpa: ")
gpa = int(gpa)
print("This is your current gpa:" , gpa)

How do i turn the gpa into a decimal so it will print as a decimal?

我如何将 gpa 转换为小数,以便将其打印为小数?

回答by Travis Griggs

Use the float() function rather than int() function.

使用 float() 函数而不是 int() 函数。

回答by thefourtheye

Change

改变

gpa = input("Enter your gpa: ")
gpa = int(gpa)

to

gpa = float(input("Enter your gpa: "))

回答by VIKASH JAISWAL

if you want to convert to decimal use:

如果要转换为十进制使用:

print("This is your current gpa:" , float(gpa))

if you want to display in some other format:

如果要以其他格式显示:

print("This is your current gpa:" , "%.2f" % gpa)

回答by MUHAMAD ABDUL HAY BIN SULAIMAN

gpa = float(input("Enter your gpa: "))
print("Your current gpa is %.2f " % gpa)