学习之数组类型转换(大写数字转换成阿拉伯数字)

2022-05-19 16:02:16   文档大全网     [ 字体: ] [ 阅读: ]

#文档大全网# 导语】以下是®文档大全网的小编为您整理的《学习之数组类型转换(大写数字转换成阿拉伯数字)》,欢迎阅读!
习之,拉伯,成阿,转换,数字
问题:1

将以"一二三四五六七八九十百千万"形式组成的数字字符串转换成int类型输出 百、千、万转换成 下标为 111213,但是大小不变还是为一百、一千、一万 int convertNumber(String chNumber){

char[] chNums = {'','','','','','','','','','','','',''};

return 0; }

实现这个方法

比如:二百三十五,转换成235

// 这里面的char[]chNums这个数组里面的内容顺序不能改变,idx对应每个内容的下标 // bitRight 意思为位元,利用位元结合下标idx,主要处理 万这四个内容 public static int convertNumber(String chNumber) {

char[] chNums = { '', '', '', '', '', '', '', '', '', '', '', '', '', '' }; if (chNumber.length() <= 0) { return 0; }

int retInt = 0; int bitRight = 1;

for (int i = chNumber.length() - 1; i >= 0; i--) { int idx = -1;

for (int j = 0; j < chNums.length; j++) {

if (chNumber.charAt(i) == chNums[j]) { idx = j; break; } }

if(idx == -1){ return -1; }

if(idx <= 9){

retInt += idx * bitRight; }

switch (idx) { case 10:

if(i != 0){

bitRight = 10; }else{

retInt += 10 * bitRight; }


break; case 11:

bitRight = 100; break; case 12:

bitRight = 1000; break; case 13:

bitRight = 10000; break; } }

return retInt; }

问题2

Int类型的一组数字转换成中文大写,例如:将521 转换为 伍佰贰拾壹

public static String numToZhStr(int number) {

char[] chNums = { '', '', '', '', '', '', '', '', '', '', '', '', '', '' }; String zhStr = ""; if (number < 0) {

number = -number; zhStr += ""; }

int[] bitRights = { 10, 100, 1000, 10000 }; for (int i = 3; i >= 0; i--) {

int br = number / bitRights[i]; number = number % bitRights[i]; if (br == 0) {

if(zhStr.length() > 0 && !zhStr.endsWith("") && !zhStr.endsWith("")){ zhStr += ""; }

continue; }

zhStr += chNums[br]; zhStr += chNums[10 + i]; }

if (number != 0) {

zhStr += chNums[number]; }


int endIdx = zhStr.length() - 1;

while(zhStr.charAt(endIdx) == chNums[0]){ endIdx--; }

if(endIdx < zhStr.length() - 1){

zhStr = zhStr.substring(0, endIdx); }

return zhStr; }


本文来源:https://www.wddqxz.cn/3bd20c9314791711cd791740.html

相关推荐