## 小写数字转换成大写数字

做这个的思路是怎样的？？我想不出来

0

#### 引用来自“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);
}```

987 = 9 * 100 + 8 * 10 + 7

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

0

10以内的：

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

System.out.print(money[0]);

0

```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);
}```

0
123 = 100 + 20 + 3  ==> 壹佰 + 贰拭 + 叁
0
http://www.oschina.net/code/snippet_223460_43461
0

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("输入货币(四舍五入):");

119        strMoney = cin.readLine();

120        money=Double.parseDouble(strMoney);

121        strChineseMoney=cm.getChineseMoney(money);

122        System.out.println(strChineseMoney);

123    }

124}

0

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))