2017-06-20 71 views
0

我刚刚进入Python,我是一个新手说至少。我在PyCharm玩耍,我正在尝试为员工数据输入添加一些基本信息。转换为货币和打印双周计划

我相信你可以看到我的脚本中的要点。无论如何,首先我已经尝试了一些想法,让它能够将薪水,奖金和年度作为货币吐出,但不能。你知道60000美元,而不是60000.

此外,如果你在第一次支付工资的日期输入,我希望它给出一个双周薪酬计划。

我正在尝试脑力风暴。

name = input('Enter Employee Name: ') 
# This should be an integer that represents the age of an employee at GPC 
try: 
    age = int(input('Enter Employee Age: ')) 
except: 
    print('Please enter a whole number') 
    exit() 
job = input('Enter Employee Job: ') 
# This should be an integer that represents the salary of the employee at 
GPC 
try: 
    salary = int(input('Enter Employee Salary to the Nearest Dollar: ')) 
except: 
    print('Please enter only numbers without foreign characters') 
    exit() 
salary_bonus = int(input('Enter employee bonus percentage '))/100 
annual_income = (salary * salary_bonus) + salary 
import datetime 
now = datetime.datetime.now() # Current Year 
u = datetime.datetime.strptime('2016-12-30','%Y-%m-%d') 
d = datetime.timedelta(weeks=2) 
t = u + d 

# Data Output 

print('Employee: ', name) 
print('Age: ', age) 
print('Job Title: ', job) 
print('Annual Salary: ', round(salary,2)) 
print('Biweekly Paycheck: ', round(salary/26, 2)) 
print('Bonus for', now.year, ': ', round(salary * salary_bonus, 2)) 
print('Actual Annual Income: ', round(annual_income, 2)) 

print('Your pay schedule will be:') 
print(t) 
print(t+d) 
+0

[转换浮动,以美元和美分(可能的重复https://stackoverflow.com/questions/21208376/converting -float-to-dollars-and-cents) – snb

+0

我试过了,并且无法让它正常工作。所以问我的脚本指导,因为我不明白为什么它不起作用。 –

+0

你是什么意思,你不能得到它的工作?它的一行......''$ {:,。2f}'。格式(1234.5)'。只需用你的号码代替1234.5,就是这样。它的格式字符串,基本上任何带{}的字符串都可以用作格式。如果您需要进一步解释* why *的作用,请转到我提供的链接。 – snb

回答

0

您可以使用local.currency

import locale 
locale.setlocale(locale.LC_ALL, '') 
print('Actual Annual Income: ', locale.currency(round(annual_income, 2), grouping=True)) 

locale.currency(VAL,符号=真,分组=假,国际= FALSE)

格式化根据当前一些VAL LC_MONETARY设置。

如果symbol为true,则返回的字符串包含货币符号,这是默认值。如果分组为真(这不是默认值),则使用该值完成分组。如果国际是真的(这不是默认),则使用国际货币符号。

请注意,此函数不适用于'C'语言环境,因此您必须先通过setlocale()设置语言环境。

编号:Python Documents: Internationalization services

PS:对于双周薪时间表,请参阅Generating recurring dates using python?

+0

这工作!谢谢! –