|
| 1 | +// 测试一下 把数字转成中文金额大写 |
| 2 | +void main() { |
| 3 | + // 输出结果: |
| 4 | + // 转换前数字: 45.12 |
| 5 | + // 转换后的中文大写: 肆拾伍元壹角贰分 |
| 6 | + // |
| 7 | + // 转换前数字: 97953164651665.123 |
| 8 | + // 转换后的中文大写: 玖拾柒万玖仟伍佰叁拾壹亿陆仟肆佰陆拾伍万壹仟陆佰陆拾伍元壹角贰分叁厘 |
| 9 | + // |
| 10 | + // 转换前数字: 25000 |
| 11 | + // 转换后的中文大写: 贰万伍仟元 |
| 12 | + // |
| 13 | + // 转换前数字: 363,5 |
| 14 | + // 转换后的中文大写: 叁仟陆佰叁拾伍元 |
| 15 | + |
| 16 | + String number = "45.12"; |
| 17 | + ConvertNumberToChineseMoneyWords numbers = |
| 18 | + new ConvertNumberToChineseMoneyWords(); |
| 19 | + print("转换前数字: " + number + " ,转换后的中文大写: " + numbers.toChinese(number) + "\n"); |
| 20 | + number = "97953164651665.123"; |
| 21 | + print("转换前数字: " + number + " ,转换后的中文大写: " + numbers.toChinese(number) + "\n"); |
| 22 | + number = "25000"; |
| 23 | + print("转换前数字: " + number + " ,转换后的中文大写: " + numbers.toChinese(number) + "\n"); |
| 24 | + number = "363,5"; |
| 25 | + print("转换前数字: " + number + " ,转换后的中文大写: " + numbers.toChinese(number) + "\n"); |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +////////////////////////////////////////////////////////////////////////////////// |
| 30 | +///////// |
| 31 | +///////// 以下才是工具类的完整内容 |
| 32 | +//////// |
| 33 | +////////////////////////////////////////////////////////////////////////////////// |
| 34 | +
|
| 35 | + |
| 36 | +/* |
| 37 | + * 把数字转换成中文金额大写工具类 |
| 38 | + */ |
| 39 | +class ConvertNumberToChineseMoneyWords { |
| 40 | + // 大写数字 |
| 41 | + List<String> NUMBERS = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"]; |
| 42 | + |
| 43 | + // 整数部分的单位 |
| 44 | + List<String> IUNIT = [ |
| 45 | + "元", |
| 46 | + "拾", |
| 47 | + "佰", |
| 48 | + "仟", |
| 49 | + "万", |
| 50 | + "拾", |
| 51 | + "佰", |
| 52 | + "仟", |
| 53 | + "亿", |
| 54 | + "拾", |
| 55 | + "佰", |
| 56 | + "仟", |
| 57 | + "万", |
| 58 | + "拾", |
| 59 | + "佰", |
| 60 | + "仟" |
| 61 | + ]; |
| 62 | + |
| 63 | + // 小数部分的单位 |
| 64 | + List<String> DUNIT = ["角", "分", "厘"]; |
| 65 | + |
| 66 | + //转成中文的大写金额 |
| 67 | + String toChinese(String str) { |
| 68 | + if (str == "0" || str == "0.00") { |
| 69 | + return "零元"; |
| 70 | + } |
| 71 | + // 去掉"," |
| 72 | + str = str.replaceAll(",", ""); |
| 73 | + // 整数部分数字 |
| 74 | + String integerStr; |
| 75 | + // 小数部分数字 |
| 76 | + String decimalStr; |
| 77 | + |
| 78 | + // 初始化:分离整数部分和小数部分 |
| 79 | + if (str.indexOf(".") > 0) { |
| 80 | + integerStr = str.substring(0, str.indexOf(".")); |
| 81 | + decimalStr = str.substring(str.indexOf(".") + 1); |
| 82 | + } else if (str.indexOf(".") == 0) { |
| 83 | + integerStr = ""; |
| 84 | + decimalStr = str.substring(1); |
| 85 | + } else { |
| 86 | + integerStr = str; |
| 87 | + decimalStr = ""; |
| 88 | + } |
| 89 | + |
| 90 | + // 超出计算能力,直接返回 |
| 91 | + if (integerStr.length > IUNIT.length) { |
| 92 | + print(str + ":超出计算能力"); |
| 93 | + return str; |
| 94 | + } |
| 95 | + |
| 96 | + // 整数部分数字 |
| 97 | + var integers = toIntArray(integerStr); |
| 98 | + // 设置万单位 |
| 99 | + bool isWan = isWanYuan(integerStr); |
| 100 | + // 小数部分数字 |
| 101 | + var decimals = toIntArray(decimalStr); |
| 102 | + // 返回最终的大写金额 |
| 103 | + return getChineseInteger(integers, isWan) + getChineseDecimal(decimals); |
| 104 | + } |
| 105 | + |
| 106 | + // 将字符串转为int数组 |
| 107 | + static List<int> toIntArray(String number) { |
| 108 | + List<int> array = []; |
| 109 | + if (array.length > number.length) { |
| 110 | + throw new Exception("数组越界异常"); |
| 111 | + } else { |
| 112 | + for (int i = 0; i < number.length; i++) { |
| 113 | + array.insert(i, int.parse(number.substring(i, i + 1))); |
| 114 | + } |
| 115 | + return array; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // 判断当前整数部分是否已经是达到【万】 |
| 120 | + static bool isWanYuan(String integerStr) { |
| 121 | + int length = integerStr.length; |
| 122 | + if (length > 4) { |
| 123 | + String subInteger = ""; |
| 124 | + if (length > 8) { |
| 125 | + subInteger = integerStr.substring(length - 8, length - 4); |
| 126 | + } else { |
| 127 | + subInteger = integerStr.substring(0, length - 4); |
| 128 | + } |
| 129 | + return int.parse(subInteger) > 0; |
| 130 | + } else { |
| 131 | + return false; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // 将整数部分转为大写的金额 |
| 136 | + String getChineseInteger(var integers, bool isWan) { |
| 137 | + StringBuffer chineseInteger = new StringBuffer(""); |
| 138 | + int length = integers.length; |
| 139 | + for (int i = 0; i < length; i++) { |
| 140 | + String key = ""; |
| 141 | + if (integers[i] == 0) { |
| 142 | + // 万(亿) |
| 143 | + if ((length - i) == 13) |
| 144 | + key = IUNIT[4]; |
| 145 | + else if ((length - i) == 9) { |
| 146 | + // 亿 |
| 147 | + key = IUNIT[8]; |
| 148 | + } else if ((length - i) == 5 && isWan) { |
| 149 | + // 万 |
| 150 | + key = IUNIT[4]; |
| 151 | + } else if ((length - i) == 1) { |
| 152 | + // 元 |
| 153 | + key = IUNIT[0]; |
| 154 | + } |
| 155 | + if ((length - i) > 1 && integers[i + 1] != 0) { |
| 156 | + key += NUMBERS[0]; |
| 157 | + } |
| 158 | + } |
| 159 | + chineseInteger.write(integers[i] == 0 |
| 160 | + ? key |
| 161 | + : (NUMBERS[integers[i]] + IUNIT[length - i - 1])); |
| 162 | + } |
| 163 | + return chineseInteger.toString(); |
| 164 | + } |
| 165 | + |
| 166 | + // 将小数部分转为大写的金额 |
| 167 | + String getChineseDecimal(var decimals) { |
| 168 | + StringBuffer chineseDecimal = new StringBuffer(""); |
| 169 | + for (int i = 0; i < decimals.length; i++) { |
| 170 | + if (i == 3) { |
| 171 | + break; |
| 172 | + } |
| 173 | + chineseDecimal |
| 174 | + .write(decimals[i] == 0 ? "" : (NUMBERS[decimals[i]] + DUNIT[i])); |
| 175 | + } |
| 176 | + return chineseDecimal.toString(); |
| 177 | + } |
| 178 | +} |
0 commit comments