number_format_util.dart 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /// 对数字或金额的格式化
  2. class NumberFormatUtil {
  3. /// 格式化金额,每三位数分割一个逗号,1000 -> 1,000
  4. static String formatMoney(int number) {
  5. List<String> parts = [];
  6. String numberString = number.toString();
  7. while (numberString.length > 3) {
  8. parts.add(numberString.substring(numberString.length - 3));
  9. numberString = numberString.substring(0, numberString.length - 3);
  10. }
  11. parts.add(numberString);
  12. return parts.reversed.join(",");
  13. }
  14. //对比带. 的版本号大小
  15. static int compareVersionNumbers(String version1, String version2) {
  16. List<int> v1 = version1.split('.').map(int.parse).toList();
  17. List<int> v2 = version2.split('.').map(int.parse).toList();
  18. int minLength = v1.length < v2.length ? v1.length : v2.length;
  19. for (int i = 0; i < minLength; i++) {
  20. if (v1[i] < v2[i]) {
  21. return -1; // version1 < version2
  22. } else if (v1[i] > v2[i]) {
  23. return 1;
  24. }
  25. }
  26. if (v1.length < v2.length) {
  27. return -1;
  28. } else if (v1.length > v2.length) {
  29. return 1; // version1 > version2
  30. }
  31. return 0; //version1 = version2
  32. }
  33. }