diff --git a/src/content/dev/theme.mdx b/src/content/dev/theme.mdx index 4c4d6db..c1bfe72 100644 --- a/src/content/dev/theme.mdx +++ b/src/content/dev/theme.mdx @@ -143,8 +143,27 @@ const days = node.expires_in !== undefined ## 价格与付款周期 -`currency` 是三个大写字母的货币代码,可以直接交给 `Intl.NumberFormat`。hub 只检查格式,不核对代码是否 -真的存在;`Intl` 遇到不认识的代码会把代码本身当作符号显示。 +`currency` 是货币代码,交给 `Intl.NumberFormat` 显示。hub 1.3.1 起写入的都是三个大写字母:只检查格式, +不核对代码是否真的存在,`Intl` 遇到不认识的代码会把代码本身当作符号显示。更早的 hub 不检查,那时存下的值 +可能不是三个字母,`Intl` 会抛错,要兜住。 + +```ts +// 地区写死 zh-CN:显示不随访客的浏览器语言变, +// 美元、港币写作 US$、HK$,日元写作 JP¥,不和人民币的 ¥ 混 +function money(amount: number, currency: string): string { + try { + return new Intl.NumberFormat("zh-CN", { + style: "currency", + currency, + // 最多两位小数,与价格的填写精度一致,日元的小数不会被取整 + maximumFractionDigits: 2, + }).format(amount) + } catch { + // hub 1.3.1 之前存下的值,可能不是三个字母 + return `${currency} ${amount.toFixed(2)}` + } +} +``` `billing_cycle` 是付款周期: