目录python
若是未作特别说明,文中的程序都是 python3 代码。函数
载入 QuantLib:code
import QuantLib as ql print(ql.__version__)
1.15
若要在 QuantLib 中对货币进行代数计算,就要将 Currency
对象转变成为一个 Money
对象。对象
Money
的构造函数有两种,都接受两个参数:it
Money(currency, value) Money(value, currency)
currency
:一个 Currency
对象;value
:一个浮点数,表示货币的数量。Money
一般不显式构造,而是用经过 Currency
对象乘一个浮点数产生:io
cny = ql.CNYCurrency() m = 123.4567 * cny
经常使用成员函数以下:class
currency()
:返回 Currency
对象,即货币;value()
:返回浮点数,即货币量;rounded()
:返回四舍五入后的 Money
对象。此外 Money
类重载了运算符,以实现基本的代数计算。import
示例,构造函数
cny = ql.CNYCurrency() m = 123.4567 * cny print(m.value()) print(m.currency()) print(m.rounded()) print(m * 10) print(m + m)
123.4567 Chinese yuan Y 123.46 Y 1234.57 Y 246.91
结果会根据货币的类型自动四舍五入。程序