NumUtils class

  • @Author: chuxiong
  • @Created at: 23-07-2025 17:32
  • @Email:
  • description 数值工具:基于 Decimal 提供防精度丢失的加减乘除与关系运算。 依赖:decimal(纯 Dart)。

命名约定:

  • *Num:入参 num,返回 double。运算抛 ArgumentError(非静默 0.0), 但最终 .toDouble() 仍有损——详见 addNum 的精度说明。
  • *Dec:入参 num,返回 String?(精确十进制字符串;Decimal 为内部实现, 不出现在公开签名——失败返回 null)。
  • *DecString:入参 / 出参均为字符串,精度最高的入口,建议金额计算优先用此族。

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

addDec(num a, num b) → String?
加 (精确相加,防止精度丢失). add (without loosing precision).
addDecString(String a, String b) → String?
两个数相加(防止精度丢失),返回十进制字符串;解析失败返回 null。
addNum(num a, num b) → double
add (without loosing precision). 两个数相加(防止精度丢失)
divideDec(num a, num b) → String?
除 (精确相除,防止精度丢失). divide (without loosing precision).
divideDecString(String a, String b) → String?
除,返回十进制字符串。
divideNum(num a, num b) → double
divide (without loosing precision). 两个数相除(防止精度丢失)
getDoubleByValueString(String valueStr, {double defValue = 0}) → double
get double by value str. 数字字符串转double
getIntByValueString(String valueStr, {int defValue = 0}) → int
get int by value string 将数字字符串转int
getNumByValueDouble(double? value, int fractionDigits) → num?
浮点数字保留x位小数
getNumByValueString(String valueStr, {int? fractionDigits}) → num?
将数字字符串转num,数字保留x位小数
greaterOrEqual(num a, num b) → bool
Relational greater than or equal operator.
greaterOrEqualDecString(String a, String b) → bool
Relational greater than or equal operator.
greaterThan(num a, num b) → bool
Relational greater than operator. 关系大于运算符。判断a是否大于b
greaterThanDecString(String a, String b) → bool
Relational greater than operator. 判断a是否大于b
isEqual(num a, num b) → bool
Checks if num a EQUAL than num b. 检查num a是否等于num b。
isGreaterThan(num a, num b) → bool
Checks if num a GREATER than num b. 检查num a是否大于num b。
isLowerThan(num a, num b) → bool
Checks if num a LOWER than num b. 检查num a是否小于num b。
isNum(String s) → bool
Checks if string is int or double. 检查字符串是int还是double
isZero(num? value) → bool
isZero 判断是否是否是0
lessThan(num a, num b) → bool
Relational less than operator. 关系小于运算符。判断a是否小于b
lessThanDecString(String a, String b) → bool
Relational less than operator. 判断a是否小于b
multiplyDec(num a, num b) → String?
乘 (精确相乘,防止精度丢失). multiply (without loosing precision).
multiplyDecString(String a, String b) → String?
乘,返回十进制字符串;解析失败返回 null。
multiplyNum(num a, num b) → double
multiply (without loosing precision). 两个数相乘(防止精度丢失)
remainder(num a, num b) → String?
余数
remainderDecString(String a, String b) → String?
余数,返回十进制字符串;解析失败或除数为 0 返回 null。
subtractDec(num a, num b) → String?
减 (精确相减,防止精度丢失). subtract (without loosing precision).
subtractDecString(String a, String b) → String?
减,返回十进制字符串;解析失败返回 null。
subtractNum(num a, num b) → double
subtract (without loosing precision). 两个数相减(防止精度丢失)
thanOrEqual(num a, num b) → bool
Relational less than or equal operator. 关系小于或等于运算符。判断a是否小于或者等于b
thanOrEqualDecString(String a, String b) → bool
Relational less than or equal operator. 判断a是否小于或者等于b