8
回答
小写数字转换成大写数字
【腾讯云】校园拼团福利,1核2G服务器10元/月!>>>   
  做这个的思路是怎样的??我想不出来
举报
共有8个答案 最后回答: 3年前

引用来自“since1986”的评论

大概想了一下,给你个思路吧(这个只是个思路,具体的各种异常处理和可能涉及的更复杂的判断你就得自己写了)

public static void main(String[] args) {
        String[] numbers = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        String[] units = {"", "拾", "佰", "仟", "万", "拾万", "佰万", "仟万", "亿"};

        String moneyLower = "987";
        StringBuffer moneyUpper = new StringBuffer();
        for(int i = 0; i < moneyLower.length(); i ++) {
            int unitIndex = 0 - (i + 1 - moneyLower.length()); // '9' 的index为 2
            int numberIndex = Integer.parseInt( moneyLower.charAt(i) + "" );

            String numberString = numbers[numberIndex];
            String unitString = units[unitIndex];

            moneyUpper.append(numberString)
                    .append(unitString);
        }
        System.out.print(moneyUpper);
    }



再给楼主解释一下,基本的思路就是

数字 = 字(number) * 单位(unit)

数字字符串 = 字字符串(numberString) + 单位字符串(unitString)

987 = 9 * 100 + 8 * 10 + 7

"玖佰捌拾柒" = ("玖" + "佰") + ("捌" + "拾") + ("柒" + "")

大概就这么个思路

不知道是不是要的这个?

10以内的:

String[] money = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾"};

System.out.print(money[0]);
--- 共有 1 条评论 ---
我就是阿本就是这种的,比如我输入:123 它就会转换成:壹佰贰拭叁 3年前 回复

大概想了一下,给你个思路吧(这个只是个思路,具体的各种异常处理和可能涉及的更复杂的判断你就得自己写了)

public static void main(String[] args) {
        String[] numbers = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        String[] units = {"", "拾", "佰", "仟", "万", "拾万", "佰万", "仟万", "亿"};

        String moneyLower = "987";
        StringBuffer moneyUpper = new StringBuffer();
        for(int i = 0; i < moneyLower.length(); i ++) {
            int unitIndex = 0 - (i + 1 - moneyLower.length()); // '9' 的index为 2
            int numberIndex = Integer.parseInt( moneyLower.charAt(i) + "" );

            String numberString = numbers[numberIndex];
            String unitString = units[unitIndex];

            moneyUpper.append(numberString)
                    .append(unitString);
        }
        System.out.print(moneyUpper);
    }



--- 共有 2 条评论 ---
since1986回复 @我就是阿本 : unitIndex 是取单位数组的索引 3年前 回复
我就是阿本我想问下int unitIndex = 0 - (i + 1 - moneyLower.length()); 这个是表示它的长度吗? 3年前 回复
截取字符吧,从最后一位按照金额位数截取,然后做个金额配置表,获取名称;拾到亿满足位数即获取名称,零到镹就直接取吧,然后组合;

我把我收藏的发出来吧,希望能用得上:

读入一个浮点数值,将其转化为中文金额的大写方式.

试验要求: 

当金额为整数时,只表示整数部分,省略小数部分,并添加"整"字. 

当金额中含有连续的0时,只需要一个"零"即可. 

10的表示方式.例如110--壹佰一拾元整,10---一拾元整 

  1import java.io.*;

  2class chineseMoney...{

  3    private String number[]=...{"","壹","贰","叁","肆","伍","陆","柒","捌","玖"};

  4    private String unit[]=...{"元","拾","佰","仟","万","拾","佰","仟","亿","拾","佰"};

  5    private String small[]=...{"角","分"};

  6    private String strNumber,strUnit,strAll;

  7

  8    private String onlyInt(int intInt)

  9    ...{

10        String strInt;

11        strInt=String.valueOf(intInt);

12        strNumber="";strUnit="";strAll="";

13        int l=strInt.length ();

14        int j,k,zeorCount;

15        zeorCount=0;

16        for (k=0;k<l;k++)

17        ...{

18            String strTemp=strInt.substring(k,k+1);

19            int intTemp=Integer.parseInt(strTemp);

20            strNumber=number[intTemp];

21            j=l-1-k;

22            strUnit=unit[j];

23            if (intTemp==0)

24            ...{

25                if (zeorCount==0)

26                ...{

27                    strUnit=strUnit.replace('拾','零');

28                    strUnit=strUnit.replace('佰','零');

29                    strUnit=strUnit.replace('仟','零');

30                    strUnit=strUnit.replace('万','零');    

31                }

32                else

33                ...{

34                    strUnit=strUnit.replaceAll("拾","");

35                    strUnit=strUnit.replaceAll("佰","");

36                    strUnit=strUnit.replaceAll("仟","");

37                    strUnit=strUnit.replaceAll("万","");        

38                }

39                zeorCount++;

40            }        

41            strAll+=strNumber+strUnit;

42        }

43        return strAll;

44    

45    }

46    

 

47    private String onlySmall(int intSmall)

48    ...{

49       

50        strNumber="";strUnit="";strAll="";

51        String strSmall,strTemp;

52        strSmall=String.valueOf(intSmall);

53        int i;

54        if (intSmall>=10)

55        ...{

56            for (i=0;i<strSmall.length();i++)

57            ...{

58                strTemp=String.valueOf(intSmall).substring(i,i+1);

59                if (Integer.parseInt(strTemp)!=0)

60                ...{

61                    strNumber=number[Integer.parseInt(strTemp)];

62                    strUnit=small[i];   

63                    strAll+=strNumber+strUnit;

64                }

65            }

66        }

67        else

68        ...{

69            if (intSmall!=0)

70            ...{

71                strNumber=number[intSmall];

72                strUnit=small[1];   

73                strAll+=strNumber+strUnit;

74            }

75        }

76

77        return strAll;

78    }

79   

80    public String getChineseMoney(double number)

81    ...{

82        //四舍五入

83        number=(number*100+0.5)/100;

84       

85        String strAll,strChineseInt,strChineseSmall,strZheng;;

86        int intInt,intSmall;

87        strChineseInt="";strChineseSmall="";strZheng="";

88       

89        //整数部分

90        intInt=(int)( number*100/100);

91        if (intInt!=0)

92        ...{

93            strChineseInt=onlyInt(intInt);

94        }

95        //小数部分

96        double temp=(number-intInt)*100*100/100;

97        //对小数部分四舍五入

98        intSmall=(int)(temp*100+0.5)/100;

99        if (intSmall!=0)

100        ...{

101            strChineseSmall=onlySmall(intSmall);

102        }

103        else

104        ...{

105            strZheng="整";

106        }

107        strAll=strChineseInt+strChineseSmall+strZheng;

108        return strAll;

109    }

110    public static void main(String args[]) throws IOException

111    ...{

112        chineseMoney cm=new chineseMoney();

113        double money;

114        String strMoney,strChineseMoney;

115        strMoney="";

116        //读取

117        System.out.println("输入货币(四舍五入):");

118        BufferedReader cin = new BufferedReader(new InputStreamReader( System.in));

119        strMoney = cin.readLine();

120        money=Double.parseDouble(strMoney);

121        strChineseMoney=cm.getChineseMoney(money);

122        System.out.println(strChineseMoney);

123    }

124}

ruby代码


CASH_MAP = (0..9).zip ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾"] 

SCALE_MAP = ['', '拾', '百', '千', '万', '拾', '百', '千', '亿', '拾'] 


#puts CASH_MAP 


def convert(num) 
if not num.instance_of? Fixnum 
puts "Error" 
return "Error" 
end 


sum = '' 
num.to_s.reverse.split('').each_with_index {|item, index| 
sum = CASH_MAP[item.to_i][1] + SCALE_MAP[index] + sum 

sum 
end 


puts(convert(961)) 
顶部