MathUtils.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package cn.cslg.pas.common.utils;
  2. import java.math.BigDecimal;
  3. import java.util.List;
  4. /**
  5. * @Author chenyu
  6. * @Date 2023/3/28
  7. */
  8. public class MathUtils {
  9. private static final String[] DIGITS = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
  10. private static final String[] UNITS = {"", "十", "百", "千"};
  11. public static double saveTwoDecimal(double value) {
  12. BigDecimal bd = new BigDecimal(value);
  13. value = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
  14. return value;
  15. }
  16. public static String fun(int n, int num) {
  17. // n 表示目标进制, num 要转换的值
  18. String str = "";
  19. int yushu; // 保存余数
  20. int shang = num; // 保存商
  21. while (shang > 0) {
  22. yushu = shang % n;
  23. shang = shang / n;
  24. // 10-15 -> a-f
  25. if (yushu > 9) {
  26. str = (char) ('a' + (yushu - 10)) + str;
  27. } else {
  28. str = yushu + str;
  29. }
  30. }
  31. return str;
  32. }
  33. public static int BinaryToDecimal(int binaryNumber) {
  34. int decimal = 0;
  35. int p = 0;
  36. while (true) {
  37. if (binaryNumber == 0) {
  38. break;
  39. } else {
  40. int temp = binaryNumber % 10;
  41. decimal += temp * Math.pow(2, p);
  42. binaryNumber = binaryNumber / 10;
  43. p++;
  44. }
  45. }
  46. return decimal;
  47. }
  48. public static Integer getNotNullMinNum(List<Integer> nums) {
  49. Integer min = 100000;
  50. for (Integer i : nums) {
  51. if (i != null) {
  52. if (i < min) {
  53. min = i;
  54. }
  55. }
  56. }
  57. return min;
  58. }
  59. public static String numberToChinese(int num) {
  60. if (num == 0) {
  61. return "零";
  62. }
  63. StringBuilder result = new StringBuilder();
  64. String numStr = Integer.toString(num);
  65. int length = numStr.length();
  66. boolean prevZero = false; // 标记前一个字符是否为零
  67. for (int i = 0; i < length; i++) {
  68. int digit = numStr.charAt(i) - '0';
  69. int unitIndex = length - i - 1; // 计算单位位置(从高位开始)
  70. if (digit == 0) {
  71. prevZero = true;
  72. } else {
  73. if (prevZero) {
  74. result.append(DIGITS[0]);
  75. prevZero = false;
  76. }
  77. result.append(DIGITS[digit]);
  78. if (unitIndex > 0) { // 个位不添加单位
  79. result.append(UNITS[unitIndex]);
  80. }
  81. prevZero = false;
  82. }
  83. }
  84. String str = result.toString();
  85. // 处理十位为1的特殊情况(10-19)
  86. if (str.startsWith("一十") && str.length() > 1) {
  87. str = str.substring(1);
  88. }
  89. // 清理多余零
  90. str = str.replaceAll("零+", "零");
  91. if (str.endsWith("零")) {
  92. str = str.substring(0, str.length() - 1);
  93. }
  94. return str;
  95. }
  96. public static String convertToChinese(int num) {
  97. if (num < 0 || num > 100) {
  98. return "";
  99. }
  100. if (num == 0) {
  101. return "零";
  102. }
  103. if (num == 100) {
  104. return "一百";
  105. }
  106. int ten = num / 10;
  107. int unit = num % 10;
  108. StringBuilder result = new StringBuilder();
  109. // 处理十位
  110. if (ten > 0) {
  111. if (ten > 1) { // 十位大于1时需添加数字
  112. result.append(DIGITS[ten]);
  113. }
  114. result.append("十");
  115. }
  116. // 处理个位
  117. if (unit > 0 || ten == 0) { // 个位为0且十位存在时不处理,否则需添加
  118. result.append(DIGITS[unit]);
  119. }
  120. return result.toString();
  121. }
  122. }