number.dart 502 B

123456789101112131415161718192021
  1. /// 数值工具类
  2. class NumUtil {
  3. NumUtil._();
  4. /// 对double四舍五入
  5. static double roundDouble(double value, [int digits = 2]) {
  6. final digitsStr = value.toStringAsFixed(digits);
  7. final result = double.parse(digitsStr);
  8. return result;
  9. }
  10. /// 在一定精度上几乎相等
  11. static bool almostEquals(
  12. double num1,
  13. double num2, [
  14. double precision = 0.000001,
  15. ]) {
  16. if (num1.isNaN && num2.isNaN) return true;
  17. return (num1 - num2).abs() <= precision;
  18. }
  19. }