Skip to content

Instantly share code, notes, and snippets.

@CaiJingLong
Created January 3, 2023 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CaiJingLong/c7a003b2c1b28e51ef68c84a45cd32c1 to your computer and use it in GitHub Desktop.
Save CaiJingLong/c7a003b2c1b28e51ef68c84a45cd32c1 to your computer and use it in GitHub Desktop.
vagrant-oak-1842

vagrant-oak-1842

Created with <3 with dartpad.cn.

// 转换数字为中文大写
// 比如:123456789.12 转换为 壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元壹角贰分
String convertNumberToChinese(double number) {
final numberText = number.toStringAsFixed(2);
final numberTextList = numberText.split('.');
final integerText = numberTextList[0];
final decimalText = numberTextList[1];
final integerTextList = integerText.split('');
final decimalTextList = decimalText.split('');
final integerTextListLength = integerTextList.length;
final decimalTextListLength = decimalTextList.length;
final integerTextListChinese = <String>[];
final decimalTextListChinese = <String>[];
final integerTextListChineseMap = {
'0': '零',
'1': '壹',
'2': '贰',
'3': '叁',
'4': '肆',
'5': '伍',
'6': '陆',
'7': '柒',
'8': '捌',
'9': '玖',
};
final decimalTextListChineseMap = {
'0': '零',
'1': '壹',
'2': '贰',
'3': '叁',
'4': '肆',
'5': '伍',
'6': '陆',
'7': '柒',
'8': '捌',
'9': '玖',
};
final integerTextListChineseUnitMap = {
0: '元',
1: '拾',
2: '佰',
3: '仟',
4: '万',
5: '拾',
6: '佰',
7: '仟',
8: '亿',
9: '拾',
10: '佰',
11: '仟',
12: '万',
13: '拾',
14: '佰',
15: '仟',
16: '亿',
17: '拾',
18: '佰',
19: '仟',
20: '万',
21: '拾',
22: '佰',
23: '仟',
24: '亿',
};
final decimalTextListChineseUnitMap = {
0: '角',
1: '分',
};
for (var i = 0; i < integerTextListLength; i++) {
final integerTextListChineseItem =
integerTextListChineseMap[integerTextList[i]];
final integerTextListChineseUnitItem =
integerTextListChineseUnitMap[integerTextListLength - i - 1];
integerTextListChinese
.add(integerTextListChineseItem! + integerTextListChineseUnitItem!);
}
for (var i = 0; i < decimalTextListLength; i++) {
final decimalTextListChineseItem =
decimalTextListChineseMap[decimalTextList[i]];
final decimalTextListChineseUnitItem = decimalTextListChineseUnitMap[i];
decimalTextListChinese
.add(decimalTextListChineseItem! + decimalTextListChineseUnitItem!);
}
final integerTextListChineseString = integerTextListChinese.join('');
final decimalTextListChineseString = decimalTextListChinese.join('');
final numberChinese =
integerTextListChineseString + decimalTextListChineseString;
return numberChinese;
}
void main(List<String> args) {
final number = 123456789.12;
final numberChinese = convertNumberToChinese(number);
print(numberChinese);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment