Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在Python中将浮点转换为字符串时保留小数点_Python 3.x - Fatal编程技术网

Python 3.x 在Python中将浮点转换为字符串时保留小数点

Python 3.x 在Python中将浮点转换为字符串时保留小数点,python-3.x,Python 3.x,我正在为学校写一个自动取款机程序。我一直在做很多研究,试图弄明白为什么当我的数字像100.00时,当转换为字符串时,它们会变成100.0。我试过很多不同的方法,但总是出错 import sys #account balance acct_bal = float(500.25) deposit_amount = float(0.00) balance = float(0.00) withdrawal_amount = float(0.00) #<--------functions go

我正在为学校写一个自动取款机程序。我一直在做很多研究,试图弄明白为什么当我的数字像100.00时,当转换为字符串时,它们会变成100.0。我试过很多不同的方法,但总是出错

import sys

#account balance
acct_bal = float(500.25)
deposit_amount = float(0.00)
balance = float(0.00)
withdrawal_amount = float(0.00)

#<--------functions go here-------------------->
#printbalance function, choice B
def account_balance(acct_bal):
    print("Your current balance:")
    print(acct_bal)

#deposit function, choice D
def deposit(deposit_amount, balance):
    print("Deposit was $" + str(float(deposit_amount)) + ", current balance is $" + str(float(balance)))
    #print("Deposit was $" + "{:,.2f}".format(deposit_amount) + ", current balance is $" + "{:,.2f}".format(balance))
    #This one causes an error
    #Traceback (most recent call last):
    #File "G:/SNHU/1 - CURRENT/IT-140 Introduction to Scripting Python (10-30-17 to 12-24-17)/Module 5/ATM", line 29, in <module>
    #deposit(deposit_amount, balance)
    #File "G:/SNHU/1 - CURRENT/IT-140 Introduction to Scripting Python (10-30-17 to 12-24-17)/Module 5/ATM", line 17, in deposit
    #print("Deposit was $" + "{:,.2f}".format(deposit_amount) + ", current balance is $" + "{:,.2f}".format(balance))
    #ValueError: Unknown format code 'f' for object of type 'str'

#withdraw function, choice W
def withdrawal(withdrawal_amount, balance):
    print("Withdrawal amount was $" + str(float(withdrawal_amount)) + ", current balance is $" + str(float(balance)))

#User Input goes here, use if/else conditional statement to call function based on user input
userchoice = input("What would you like to do?\n")

if (userchoice == "D"):
    deposit_amount = input("How much would you like to deposit today?\n")
    balance = acct_bal + float(deposit_amount)
    deposit(deposit_amount, balance)
elif (userchoice == "B"):
    account_balance(acct_bal)
else:
    withdrawal_amount = input("How much would you like to withdraw?\n")
    balance = acct_bal - float(withdrawal_amount)
    withdrawal(withdrawal_amount, balance)
应该是>

Withdrawal amount was $100.00, current balance is $400.25

如果您使用的是Python3.x,那么有一种简单的方法可以完成这个过程,退出代码为0

value = 100.000

#the .2f specifys the number of trailing values after the decimal you want
final = "{:.2f}".format(value)

print(final)
#output:100.00
#NOTE: it will be in string
#if you want more formatting options you can check the python docs

可能重复使用带有
{:,.2f}.format(…)
的注释掉的行,并将数字而不是字符串传递给
存款
函数。(您需要将
输入的结果
转换为一个数字。)或者,
打印(“存款为$%.2f,当前余额为$%.2f”)%(存款金额,余额))
。最简单的方法是使用
str的
格式
功能。我得到以下错误:回溯(最近一次调用):文件“G:/SNHU/1-当前/IT-140脚本Python简介(10-30-17至12-24-17)/模块5/ATM”,第44行,存款(存款金额,余额)文件“G:/SNHU/1-当前/IT-140脚本Python简介(10-30-17至12-24-17)/模块5/ATM”,第20行,存款金额=“{:,.2f}”。格式(存款金额)ValueError:类型为“str”的对象的未知格式代码“f”为什么在“{:','.2f}”处有逗号
value = 100.000

#the .2f specifys the number of trailing values after the decimal you want
final = "{:.2f}".format(value)

print(final)
#output:100.00
#NOTE: it will be in string
#if you want more formatting options you can check the python docs