金额大小写转化_金额工具类

r囧r小猫 2023-08-17 17:04 141阅读 0赞

1、金额逗号分隔

  1. public class DecimalUtils {
  2. public static final DecimalFormat FORMAT = new DecimalFormat("#,##0.00");
  3. /** * @param decimal 71015000009826 * @return 71,015,000,009,826.00 */
  4. public static String format(BigDecimal decimal ,DecimalFormat format) {
  5. return format.format(decimal);
  6. }
  7. }

2、金额大小写转化

  1. package com.fintech.scf.utils;
  2. import java.math.BigDecimal;
  3. /** * @author HealerJean * @ClassName MoneyUtils * @date 2019/8/14 20:41. * @Description 传入的金额单位为分 */
  4. public class MoneyUtils {
  5. /** 金额的精度,默认值为2 四舍五入 */
  6. private static final int MONEY_PRECISION = 2;
  7. private static final String[] CN_UPPER_NUMBER = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
  8. private static final String[] CN_UPPER_MONETRAY_UNIT = { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾", "佰", "仟"};
  9. private static final String CN_NEGATIVE = "负";
  10. private static final String CN_FULL = "整";
  11. private static final String CN_ZEOR_FULL = "零元整";
  12. /** * @param money 传入的单位为元 * @return */
  13. public static String toUpper(BigDecimal money) {
  14. StringBuffer sb = new StringBuffer();
  15. int signum = money.signum();
  16. // 零元整的情况
  17. if (signum == 0) {
  18. return CN_ZEOR_FULL;
  19. }
  20. //这里会进行金额的四舍五入
  21. long number = money.movePointRight(MONEY_PRECISION) .setScale(0, 4).abs().longValue();
  22. // 得到小数点后两位值
  23. long scale = number % 100;
  24. int numUnit = 0;
  25. int numIndex = 0;
  26. boolean getZero = false;
  27. // 判断最后两位数,一共有四中情况:00 = 0, 01 = 1, 10, 11
  28. if (!(scale > 0)) {
  29. numIndex = 2;
  30. number = number / 100;
  31. getZero = true;
  32. }
  33. if ((scale > 0) && (!(scale % 10 > 0))) {
  34. numIndex = 1;
  35. number = number / 10;
  36. getZero = true;
  37. }
  38. int zeroSize = 0;
  39. while (true) {
  40. if (number <= 0) {
  41. break;
  42. }
  43. // 每次获取到最后一个数
  44. numUnit = (int) (number % 10);
  45. if (numUnit > 0) {
  46. if ((numIndex == 9) && (zeroSize >= 3)) {
  47. sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
  48. }
  49. if ((numIndex == 13) && (zeroSize >= 3)) {
  50. sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
  51. }
  52. sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
  53. sb.insert(0, CN_UPPER_NUMBER[numUnit]);
  54. getZero = false;
  55. zeroSize = 0;
  56. } else {
  57. ++zeroSize;
  58. if (!(getZero)) {
  59. sb.insert(0, CN_UPPER_NUMBER[numUnit]);
  60. }
  61. if (numIndex == 2) {
  62. if (number > 0) {
  63. sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
  64. }
  65. } else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
  66. sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
  67. }
  68. getZero = true;
  69. }
  70. // 让number每次都去掉最后一个数
  71. number = number / 10;
  72. ++numIndex;
  73. }
  74. // 如果signum == -1,则说明输入的数字为负数,就在最前面追加特殊字符:负
  75. if (signum == -1) {
  76. sb.insert(0, CN_NEGATIVE);
  77. }
  78. // 输入的数字小数点后两位为"00"的情况,则要在最后追加特殊字符:整
  79. if (scale <= 0) {
  80. sb.append(CN_FULL);
  81. }
  82. return sb.toString();
  83. }
  84. public static void main(String[] args) {
  85. BigDecimal numberOfMoney = new BigDecimal("23000000.00");
  86. String s = toUpper(numberOfMoney);
  87. System.out.println("你输入的金额为:【" + numberOfMoney + "】 => " + s + " ");
  88. }
  89. }

ContactAuthor

发表评论

表情:
评论列表 (有 0 条评论,141人围观)

还没有评论,来说两句吧...

相关阅读