number_util.dart 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import 'dart:math';
  2. class NumberUtil {
  3. static String format(double n) {
  4. if (n >= 1000000000) {
  5. n /= 1000000000;
  6. return "${n.toStringAsFixed(2)}B";
  7. } else if (n >= 1000000) {
  8. n /= 1000000;
  9. return "${n.toStringAsFixed(2)}M";
  10. } else if (n >= 10000) {
  11. n /= 1000;
  12. return "${n.toStringAsFixed(2)}K";
  13. } else {
  14. return n.toStringAsFixed(4);
  15. }
  16. }
  17. static int getDecimalLength(double b) {
  18. String s = b.toString();
  19. int dotIndex = s.indexOf(".");
  20. if (dotIndex < 0) {
  21. return 0;
  22. } else {
  23. return s.length - dotIndex - 1;
  24. }
  25. }
  26. static int getMaxDecimalLength(double a, double b, double c, double d) {
  27. int result = max(getDecimalLength(a), getDecimalLength(b));
  28. result = max(result, getDecimalLength(c));
  29. result = max(result, getDecimalLength(d));
  30. return result;
  31. }
  32. static bool checkNotNullOrZero(double? a) {
  33. if (a == null || a == 0) {
  34. return false;
  35. } else if (a.abs().toStringAsFixed(4) == "0.0000") {
  36. return false;
  37. } else {
  38. return true;
  39. }
  40. }
  41. }