You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.3 KiB

  1. package com.spring.modules.part.util;
  2. import java.math.BigDecimal;
  3. public class UtilsClass {
  4. /**
  5. * 字符串转BigDecimal
  6. * @param str
  7. * @return
  8. */
  9. public static BigDecimal stringToBigDecimal(String str) {
  10. if (str == null || str.trim().isEmpty()) {
  11. return BigDecimal.ZERO; // 或者返回 BigDecimal.ZERO,视业务需求而定
  12. }
  13. try {
  14. return new BigDecimal(str.trim());
  15. } catch (Exception ex) {
  16. System.err.println("无法解析的数值: " + str);
  17. return BigDecimal.ZERO; // 或抛异常,或返回 null
  18. }
  19. }
  20. /**
  21. * 字符串转Integer
  22. * @param str
  23. * @return
  24. */
  25. public static int stringToInteger(String str) {
  26. // 初始化一个默认的整数值
  27. int number = 0;
  28. // 检查传入的字符串是否为 null
  29. if (str == null) {
  30. return number; // 如果是 null,则返回默认值
  31. }
  32. try {
  33. // 使用字符串创建 Integer 对象
  34. number = Integer.parseInt(str);
  35. } catch (NumberFormatException e) {
  36. // 处理非法格式的情况
  37. System.err.println("无效的整数值: " + str);
  38. // 返回默认值
  39. number = 0;
  40. }
  41. return number;
  42. }
  43. }