Skip to main content

djagngo 的简单货币处理

项目描述

概述

处理基本货币处理、格式化和 手动添加汇率的简单 django 应用程序,可用于轻松从一种货币转换为另一种货币

示例用法

from currency.models import Currency, ExchangeRate, Money

usd = Currency.objects.create(code='USD', short_name=u'$')
eur = Currency.objects.create(code='EUR', short_name=u'€')

ExchangeRate.objects.create(base_currency=usd, foreign_currency=eur, rate=1/1.3)

print(usd.get_rate(eur))  # Decimal('0.76923')
print(eur.get_rate(usd))  # Decimal('1.30000')

my_money = Money(1531, 'USD')
print(my_money)  # 1531.00000USD
my_money += Money(23, 'USD')
print(my_money)  # 1554.00000USD
print(my_money.convert_to('EUR'))  # 1195.38342EUR

# be careful with conversions. Errors accumulate with each conversion. Example:
print(my_money.convert_to('EUR').convert_to('USD'))  # 1553.99845USD


# indirect rates through rates of default currency (USD) are available too
hrn = Currency.objects.create(code='UAH', short_name='hrn')
rub = Currency.objects.create(code='RUB', short_name='rub')

ExchangeRate.objects.create(
    base_currency=default_currency, foreign_currency=hrn,
    rate='0.125')
# get stored value:
rate1 = ExchangeRate.objects.get(
    base_currency=default_currency, foreign_currency=hrn)

ExchangeRate.objects.create(
    base_currency=default_currency, foreign_currency=rub,
    rate='0.03125')
# get stored value:
rate2 = ExchangeRate.objects.get(
    base_currency=default_currency, foreign_currency=rub)

self.assertEqual(hrn.get_rate(rub), rate1.rate / rate2.rate)

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

django-currency-0.0.8.tar.gz (7.8 kB 查看哈希)

已上传 source

内置分布

django-currency-0.0.8.linux-x86_64.tar.gz (16.0 kB 查看哈希)

已上传 any