Money class

Implemented types

Constructors

Money.fromCents(int? cents, {PrecisionMode precisionMode = PrecisionMode.round, NullValueHandling nullValueHandling = NullValueHandling.returnZero})
从分初始化(无精度损失,推荐)
factory
Money.fromString(String? yuanStr, {PrecisionMode precisionMode = PrecisionMode.round, NullValueHandling nullValueHandling = NullValueHandling.returnZero})
从字符串初始化(支持空字符串、带空格、货币符号)
factory
Money.fromYuan(num? yuan, {PrecisionMode precisionMode = PrecisionMode.round, NullValueHandling nullValueHandling = NullValueHandling.returnZero})
从元初始化(支持 int/double/null,可配置空值处理)
factory

Properties

hashCode int
The hash code for this object.
no setteroverride
isNegative bool
是否为负数(严格小于0)
no setter
isPositive bool
是否为正数(严格大于0)
no setter
isZero bool
是否为零
no setter
precisionMode PrecisionMode
精度模式(取整方式)
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

abs() Money
绝对值(如 -50 元 → 50 元)
compareTo(Money other) int
Compares this object to another object.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toCents() int
转换为分(int)
toDouble() double
转换为double(同toYuan())
toInt() int
转换为int(元为单位,向下取整)
toJson({bool asCents = false}) Map<String, dynamic>
序列化为 JSON(包含金额和精度模式,确保反序列化一致)
toNum() num
转换为 num(支持直接赋值给 num 类型变量,显式调用)
toString() String
A string representation of this object.
override
toYuan() double
转换为元(double,按精度模式保留2位小数)
toYuanString({int maxDigits = 2, bool keepFixedDigits = false}) String
转换为元字符串(自动补零,格式:"100.00")

Operators

operator %(num divisor) Money
Money % int/double(取模运算,结果保留分)
operator *(num multiplier) Money
Money * int/double(按当前精度模式取整)
operator +(Money other) Money
Money + Money
operator -(Money other) Money
Money - Money
operator /(num divisor) Money
Money / int/double(按当前精度模式取整)
operator <(Money other) bool
Money < Money
operator <=(Money other) bool
Money <= Money
operator ==(Object other) bool
The equality operator.
override
operator >(Money other) bool
Money > Money
operator >=(Money other) bool
Money >= Money
operator unary-() Money
相反数(如 100 元 → -100 元)

Static Properties

zero Money
final

Static Methods

fromJson(dynamic json, {PrecisionMode? precisionMode}) Money
从 JSON 解析(支持恢复精度模式)