hutool.core.money

货币计算工具类

提供元/分转换和含税/不含税价格计算功能。 所有计算使用 Decimal 精确运算,避免浮点精度问题。

Module Contents

Classes

MoneyUtil

货币计算工具类。

API

class hutool.core.money.MoneyUtil

货币计算工具类。

提供元(Yuan)/ 分(Fen)转换和含税 / 不含税价格计算, 默认增值税率为 13%(中国标准增值税率)。 所有计算使用 Decimal 精确运算。

DEFAULT_TAX_RATE: decimal.Decimal = 'Decimal(...)'
static fen_to_yuan(amount: int | str | decimal.Decimal) decimal.Decimal

分转元。

参数:

amount – 分金额(整数、字符串或 Decimal)

返回:

元金额(Decimal,保留两位小数)

>>> MoneyUtil.fen_to_yuan(100)
Decimal('1.00')
>>> MoneyUtil.fen_to_yuan(1)
Decimal('0.01')
>>> MoneyUtil.fen_to_yuan('50')
Decimal('0.50')
static yuan_to_fen(amount: float | str | decimal.Decimal) int

元转分(四舍五入到分)。

参数:

amount – 元金额

返回:

分金额(整数)

>>> MoneyUtil.yuan_to_fen(1)
100
>>> MoneyUtil.yuan_to_fen('0.5')
50
>>> MoneyUtil.yuan_to_fen('0.01')
1
>>> MoneyUtil.yuan_to_fen('0.001')
0
static net_price(amount: float | str | decimal.Decimal, tax_rate: float | None = None) decimal.Decimal

根据含税价计算不含税价(去税价)。

公式:不含税价 = 含税价 / (1 + 税率/100)

参数:
  • amount – 含税价金额

  • tax_rate – 税率百分比,默认为 13%

返回:

不含税价(Decimal,保留两位小数)

抛出:

ValueError – 税率大于等于 100%

>>> MoneyUtil.net_price(Decimal('113'))
Decimal('100.00')
>>> MoneyUtil.net_price(Decimal('108'), tax_rate=8)
Decimal('100.00')
static gross_price(amount: float | str | decimal.Decimal, tax_rate: float | None = None) decimal.Decimal

根据不含税价计算含税价。

公式:含税价 = 不含税价 × (1 + 税率/100)

参数:
  • amount – 不含税价金额

  • tax_rate – 税率百分比,默认为 13%

返回:

含税价(Decimal,保留两位小数)

抛出:

ValueError – 税率大于等于 100%

>>> MoneyUtil.gross_price(Decimal('100'))
Decimal('113.00')
>>> MoneyUtil.gross_price(Decimal('100'), tax_rate=8)
Decimal('108.00')
static tax_amount(amount: float | str | decimal.Decimal, tax_rate: float | None = None) decimal.Decimal

计算税额(含税价中的税额部分)。

公式:税额 = 含税价 - 不含税价

参数:
  • amount – 含税价金额

  • tax_rate – 税率百分比,默认为 13%

返回:

税额(Decimal,保留两位小数)

抛出:

ValueError – 税率大于等于 100%

>>> MoneyUtil.tax_amount(Decimal('113'))
Decimal('13.00')
static _resolve_tax_rate(tax_rate: float | None) decimal.Decimal

解析税率参数,返回 Decimal。

static netto(amount, tax_rate=None)

从含税价计算不含税价(别名 netto,对应 huTools)。

参数:
  • amount – 含税价金额

  • tax_rate – 税率百分比

返回:

不含税价(Decimal)

static brutto(amount, tax_rate=None)

从不含税价计算含税价(别名 brutto,对应 huTools)。

参数:
  • amount – 不含税价金额

  • tax_rate – 税率百分比

返回:

含税价(Decimal)

static profit_margin(cost, selling_price)

计算利润率。

公式:利润率 = (售价 - 成本) / 成本 × 100

参数:
  • cost – 成本价

  • selling_price – 售价

返回:

利润率百分比(Decimal)

抛出:

ValueError – 成本价为 0 时

static cent_to_yuan(cent)

分转元(别名,与 fen_to_yuan 等价)。

参数:

cent – 分金额

返回:

元金额(Decimal)

static yuan_to_cent(yuan)

元转分(别名,与 yuan_to_fen 等价)。

参数:

yuan – 元金额

返回:

分金额(int)