普贤教程 › 包参考
currency 包
货币转换
| 项 | 值 |
|---|---|
| 包名 | currency |
| 版本 | 0.2.0 |
| 可用版本 | 0.1.0 / 0.2.0 |
| 入口 | currency.px |
| 分类 | 数学与统计 |
| 文件数 / 行数 | 1 / 201 |
安装与引入
bash
pxpkg init # 首次 pxpkg add currency@0.2.0 pxpkg install
px
import currency所属分类教程:数学与统计
公开接口
| 函数 | 参数 |
|---|---|
cur_pair | from_code, to_code |
cur_new | rates |
cur_add_rate | c, from_code, to_code, rate |
cur_rates | c |
cur_list | c |
cur_has_rate | c, from_code, to_code |
cur_convert | c, from_code, to_code, amount |
cur_convert_ok | c, from_code, to_code, amount |
cur_missing | c, from_code, to_code |
cur_format | amount, code |
cur_format_thousands | amount, code, sep = "," |
cur_round | amount |
cur_format_signed | amount, code |
其余 4 个 def(内部与辅助,可直接调用但不属稳定接口)
| 函数 | 参数 |
|---|---|
cur_norm_key | k |
_cur_is_pair_key | k |
_cur_rate | c, f, t |
_cur_pad2 | n |
包内说明(源码头注释)
text
============================================================ 普贤生态库:registry/currency — 货币转换 ------------------------------------------------------------ 纯函数库,双模式一致。 实现要点: - 键归一化:cur_new() / cur_add_rate() 接受 "USD->CNY" / "USD/CNY" / "USD CNY" 三种写法(大小写不敏感), 嵌套写法 {"USD": {"CNY": 7.1}} 自动展开成 "USD->CNY"。 - 新增 cur_convert_ok() → Ok(金额) | Err("缺少汇率: USD->JPY"), 以及 cur_has_rate() / cur_rates() / cur_missing() 便于自查。 - cur_format() 改为按分计算、四舍五入、固定两位小数; 新增 cur_format_thousands() 带千分位。 函数: cur_new(rates) / cur_add_rate(c, from, to, rate) / cur_rates(c) cur_pair(from, to) → "USD->CNY" cur_has_rate(c, from, to) → bool(含反向与经 USD 套算) cur_convert(c, from, to, amt) → float | null cur_convert_ok(c, from, to, amt) → Ok(float) | Err(msg) cur_format(amount, code) / cur_format_thousands(amount, code, sep) cur_round(amount) / cur_format_signed(amount, code) / cur_list(c) ============================================================