用于处理现金的简单、轻量级数据类型(美元)
easydollar的Python项目详细描述
v1.0.2的新增功能
- 改进了easydollar.usd()的解析,允许使用逗号(例如“$1000.00”)
- 更改了美元的输出。repr以提高可读性
- 修正了usd.distribute()中的一个主要逻辑错误。它现在应该按预期工作。
示例
cash=usd('$5,000.67')print(repr(cash))# Output: USD(dollars=5000, cents=67)
轻松py
作者肖恩·富兰克林(sean.patrick516@gmail.com)
pip install easydollar
浮点值不应在货币计算中使用。 java中python的decimal或bigdecimal使用起来有点笨拙 原型制作。
此模块对于str-解析的美元金额(来自用户输入,或a.txt/.csv/.xls)非常有用
这是错误需要三个或更多小数点的分计算模块(如天然气价格或某些APR计算)
注释 “使用float实例化USD”是故意省略的功能。像50.20这样的输入实际上变成了50.2,然后变成$50.02 ……这显然是不正确的。
如何使用美元类型
注释 通过编写
importeasydollar.examples
如何导入
建议您使用小写USD() 实例化美元实例。
usd()将str转换为usd实例。
fromeasydollar.USDimportusd
加上现金金额
示例1
husband_income=usd('55000.00')# if it's a whole dollar amount, the decimal-point is optional.wife_income=usd('62000')household_income=husband_income+wife_incomeprint(f'Total household income: {household_income}\n')
示例2
# USD instances will implicitly roll over cents into dollars when cast to a string.money1=usd('1.50')money2=usd('0.50')total=money1+money2print(f'Dollar addition: {money1} + {money2} = {total}')print(f"Output of usd('0.5255') = {usd('0.5255')}")# Outputs "$52.55"print(f"Output of usd('1000575.100') = {usd('1000575.100')}")# Outputs "$1,000,576.00"
对交易进行更改
price=usd('56.60')paid=usd('60.00')change=paid-priceprint(f'Price: {price}')print(f'Paid: {paid}')print(f'Change due: {change}')
重要!
USD对象上的乘法运算符是“scale”操作(仅接受整数) 两美元不能相乘。
payrate=usd('15.00')hours_worked=40paycheck=payrate*hours_workedprint(f'This weeks earnings: {paycheck}')
除法和利息乘法
usd中的divide运算符是一个distribution函数。
与multiply运算符类似,美元实例只能“除以”整数。
重要!
除法运算符不是true division(在某些情况下,这将涉及使用浮点值)而是调用美元的“distribute”方法。
usd.distribute(n)将usd-实例的值分布在n之间,并返回usd的^{str1}$list
如果你对列表中的元素求和,你会得到
pre-distribute()值 divide“/”运算符是onlymyus.distribute(n)[0] 如果将“/”运算符的结果n次相加,则可能不会
获取原始值。 在这里使用除法算符是可以的,因为这是第一次付款的计算。
相当于 要查找当前付款,您可以执行以下操作: 或 正在开发一个简化此过程的功能。重要!
loan_amount=usd('10653.26')interest=21# 21 percent (21%)total_loan_interest=loan_amount.interest(interest)term=60monthly_principle=loan_amount/termmonthly_interest=total_loan_interest/termfirst_payment=monthly_interest+monthly_principleprint('~~ Loan Issued. ~~')print(f'Loan Amount: {loan_amount}')print(f'Interest: {interest}%')print(f'Term: {term} months')print(f'Total interest to be paid over term: {total_loan_interest}\n')print(f'Principle monthly: {monthly_principle}')print(f'Interest monthly: {monthly_interest}\n')print(f'First payment due: {first_payment}')
monthly_principle=loan_amount.distribute(term)[0]
current_payment=my_usd.distribute(total_term)[payments_already_made]
current_payment=my_usd.distribute(remaining_term)[0]
其他注意事项:
有利息(百分比)
appreciated_value=my_usd.with_interest(50)# 50% appreciation
申请利息(百分比)
my_usd.apply_interest(0.6)# Applies 0.6% interest to my_usd
推荐PyPI第三方库