华为面试题: 人民币数字转化为文字

神勇无敌.ZXF 2007-12-01 05:10:20
人民币数字转化为文字, 如1235, 输出一千二百三十五元
...全文
1219 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
JackRio 2007-12-16
  • 打赏
  • 举报
回复


public class NumChange
{
public static void main(String args[])
{
String theStr_0="";
int theNum_0 = 0;
String theStr_1="";
theStr_0 = args[0];
int j=theStr_0.length()-1;
String arr[] = null;
for(int i=0;i<theStr_0.length();i++)
{
theNum_0 = Integer.parseInt(theStr_0.substring(i,i+1));
switch(theNum_0)
{
case 1:theStr_1+="一";break;
case 2:theStr_1+="二";break;
case 3:theStr_1+="三";break;
case 4:theStr_1+="四";break;
case 5:theStr_1+="五";break;
case 6:theStr_1+="六";break;
case 7:theStr_1+="七";break;
case 8:theStr_1+="八";break;
case 9:theStr_1+="九";break;
case 0:theStr_1+="零";break;
}
if(theNum_0!=0)
{
switch(j)
{
case 1:theStr_1+="十";break;
case 2:theStr_1+="百";break;
case 3:theStr_1+="千";break;
case 5:theStr_1+="十";break;
case 6:theStr_1+="百";break;
case 7:theStr_1+="千";break;
case 9:theStr_1+="十";break;
case 10:theStr_1+="百";break;
case 11:theStr_1+="千";break;
}
}
if(j==4)
{
theStr_1+="万";
}
if(j==8)
{
theStr_1+="亿";
}
j--;
}
theStr_1 = theStr_1.replaceAll("零+", "零");
if(theStr_1.substring(theStr_1.length()-1,theStr_1.length()).equals("零"))
{
theStr_1 = theStr_1.substring(0,theStr_1.length()-1);
}
theStr_1 = theStr_1.replaceAll("零万", "万");
theStr_1 = theStr_1.replaceAll("零亿", "亿");
System.out.println(theStr_1);
}
}
不会玩正则表达式,差不多拉,呵呵
null1null 2007-12-15
  • 打赏
  • 举报
回复
import java.text.DecimalFormat;
import java.util.Scanner;

public class ChineseCurrency {
public static void main(String[] args) {

Scanner scan = new Scanner(System.in);
System.out.println("please input the price:");
double number = scan.nextDouble();

System.out.println(toChineseCurrency(new Double(number)));
}

public static String toChineseCurrency(Object o) {
if(o instanceof Number) {
String s = new DecimalFormat("#.00").format(o);
System.out.println(s);
s = s.replaceAll("\\.", "");
char[] digit = { '零', '壹', '贰', '叁', '肆',
'伍', '陆', '柒', '捌', '玖'};
String unit = "仟佰拾兆仟佰拾亿仟佰拾万仟佰拾元角分";
int l = unit.length();
StringBuffer sb = new StringBuffer(unit);
for(int i=s.length()-1; i >=0; i--)
sb = sb.insert(l-s.length()+i, digit[(s.charAt(i) - 0x30)]);
s = sb.substring(l-s.length(), l+s.length());
s = s.replaceAll("零[拾佰仟]", "零").
replaceAll("零{2,}", "零").
replaceAll("零([兆万元])", "$1").
replaceAll("零[角分]", "");
return s;
} else {
throw new NumberFormatException();
}
}}
Tony2251 2007-12-15
  • 打赏
  • 举报
回复
学习中~~~~~~~~~~~
wuer2008 2007-12-15
  • 打赏
  • 举报
回复
这个题到处都有代码 用map还是不错的
hanfengthinker 2007-12-15
  • 打赏
  • 举报
回复
都很不错
顶下学习
jamyhyy 2007-12-12
  • 打赏
  • 举报
回复
import java.util.Stack;

public class change {

private int num;

public change(int num){
this.num=num;
}
public String c1 (int num){
String a[]={"零","一","二","三","四","五","六","七","八","九","十"};
return a[num];
}

public String c2 (int pos){
String b[]={"元","十","百","千","万","十","百","千","意","十","百","千"};
return b[pos];
}
void modify()
{
Stack<String> stack=new Stack<String>();

int len=String.valueOf(num).length();
int m=0;
while(len>0){
int index = (int) num % 10;//得到各个位数上的数值
num=num/10;

stack.push(c2(m));

stack.push(c1(index));
len--;
m++;
}

String outp="";
while(!stack.empty()){

outp+=stack.pop();
}
String out1=outp.replaceAll("零+千", "零");
String out2=out1.replaceAll("零+百", "零");
String out3=out2.replaceAll("零+意", "意");
String out4=out3.replaceAll("零+十", "零");
String out5=out4.replaceAll("零+万", "万");
String out6=out5.replaceAll("零元", "元");
String out7=out6.replaceAll("意万", "意");
String out8=out7.replaceAll("^一十", "十");
System.out.print(out8);
}

public static void main(String[] args) {

change reader = new change(564871003);
reader.modify();
}

}
weijiepeng 2007-12-11
  • 打赏
  • 举报
回复
public class numberToChineseTest {

/** Creates a new instance of numberToChineseTest */
public numberToChineseTest() {
}
@Test(groups = {"fast"})
public void numberToChinese(){
String number="6546378974";
StringBuffer strnum=new StringBuffer();
String number2=null;
Map <Integer,String> nummap=new HashMap();
Map <String ,String>stringmap=new HashMap();
nummap.put(1,"十");
nummap.put(2,"百");
nummap.put(3,"千");
nummap.put(4,"万");
nummap.put(5,"十");
nummap.put(6,"百");
nummap.put(7,"千");
nummap.put(8,"亿");
nummap.put(9,"十");
nummap.put(10,"百");
nummap.put(11,"千");
stringmap.put("1","一");
stringmap.put("2","二");
stringmap.put("3","三");
stringmap.put("4","四");
stringmap.put("5","五");
stringmap.put("6","六");
stringmap.put("8","八");
stringmap.put("9","九");
stringmap.put("0","零");
stringmap.put("7","七");
for (int i = 0; i < number.length(); i++) {
number2=number2.valueOf(number.charAt(i));
Set<String> se=stringmap.keySet();
Iterator<String> it =se.iterator();
while (it.hasNext()){
String key =it.next();
if(number2.equals(key)){
strnum.append(stringmap.get(key));
Integer key2=number.length()-1-i;
if(key2!=0){
strnum.append(nummap.get(key2));
}


}
}

}

System.out.println(strnum.toString()+"元整");

}

}
xfzhang83 2007-12-05
  • 打赏
  • 举报
回复
比较了自己的和17楼的程序,17楼的到了角分,这个我没有写,只考虑正整数.但17楼的在正确率和性能上较差.
xfzhang83 2007-12-05
  • 打赏
  • 举报
回复
应该要注意一些特殊的地方,比如零的处理,十元而不是十零元,十亿而不是一十亿,等情况,而且要注意不要限定金额长度,像15楼的"仟佰拾兆仟佰拾亿仟佰拾万仟佰拾元角分"不可取,还应注意运行的效率.replaceAll方法用多了,性能会慢
public class NumberToChinese {
public static void main(String[] args) {
NumberToChinese t = new NumberToChinese();
long t1 = System.currentTimeMillis();
String[] num = new String[5];
num[0] = "1234567890";
num[1] = "1000000000";
num[2] = "1000500022";
num[3] = "1000000000000000000";
num[4] = "10000000000000";
for(int j =0;j<10000;j++){
for (int i = 0; i < num.length; i++) {
t.conversion(num[i]);
}
}
long t2 = System.currentTimeMillis() - t1 ;
System.out.println("运算时间:"+t2);
}

public void conversion(String money){
String[] cn = {"零","一","二","三","四","五","六","七","八","九"};
String[] unit = {"十","百","千"};
StringBuffer cnMoney = new StringBuffer();
int m ;
int b = 0;
int le = money.length();
for(int i = 0;i<le;i++){
m = Integer.parseInt( money.substring(i,i+1) );
b += m ;
if( !(m == 0 && cnMoney.charAt(cnMoney.length()-1) == '零') ){
cnMoney.append(cn[m]);
}
int j = (money.length()-i-1) % 4 ;
if( j == 0 ){
if (cnMoney.charAt(cnMoney.length() - 1) == '零') {
cnMoney.deleteCharAt(cnMoney.length() - 1);
}
if( (money.length()-i-1)/4%2==0){
cnMoney.append("亿");
}else{
if(b != 0){
cnMoney.append("万");
}
}
b = 0 ;
}else{
if(m != 0){
cnMoney.append(unit[j-1]);
}
}
}
if(cnMoney.charAt(0)=='一' && cnMoney.charAt(1)=='十'){
cnMoney.deleteCharAt(0);
}
cnMoney.replace(cnMoney.length()-1, cnMoney.length(), "元");
System.out.println(money+" "+cnMoney);
}
}
yangchangye 2007-12-04
  • 打赏
  • 举报
回复

woody724 2007-12-04
  • 打赏
  • 举报
回复
正则表达式功能很好,很强大啊
ooo19841080xinxin 2007-12-03
  • 打赏
  • 举报
回复
woody724 2007-12-03
  • 打赏
  • 举报
回复
public class Reader {
private String strNum;
private String strNumChFormat;
private String strNumTemp;
private int intNumLen;
private String strBegin;
public Reader(String strNum) {
this.strNum = strNum;
}
public boolean check(String strNum) {
boolean valid = false;

if (strNum.substring(0,1).equals("0")){
this.strNum = strNum.substring(1);
}
try {
new Double(strNum);
valid = true;
}
catch (NumberFormatException ex) {
System.out.println("Bad number format!");
}
return valid;
}
public void init() {
strNumChFormat = "";
intNumLen = strNum.length();
strNumTemp = strNum;
strNumTemp = strNumTemp.replace('1', '一');
strNumTemp = strNumTemp.replace('2', '二');
strNumTemp = strNumTemp.replace('3', '三');
strNumTemp = strNumTemp.replace('4', '四');
strNumTemp = strNumTemp.replace('5', '五');
strNumTemp = strNumTemp.replace('6', '六');
strNumTemp = strNumTemp.replace('7', '七');
strNumTemp = strNumTemp.replace('8', '八');
strNumTemp = strNumTemp.replace('9', '九');
strNumTemp = strNumTemp.replace('0', '零');
strNumTemp = strNumTemp.replace('.', '点');
strBegin = strNumTemp.substring(0, 1);
}
public String readNum() {
if (check(strNum)) {
init();
try {
for (int i = 1, j = 1, k = 1; i < intNumLen; i++) {
if (strNumTemp.charAt(intNumLen - 1) == '零' && i == 1) {
strNumChFormat = "位";
}
else if (strNumTemp.charAt(intNumLen - i) == '零' && j == 1) {
strNumChFormat = "位" + strNumChFormat;
}
else if (strNumTemp.charAt(intNumLen - i) == '点') {
j = 1;
k = 1;
strNumChFormat = strNumTemp.charAt(intNumLen - i) + strNumChFormat;
continue;
}
else {
strNumChFormat = strNumTemp.charAt(intNumLen - i) + strNumChFormat;
}
if (strNumTemp.charAt(intNumLen - i - 1) != '位' &&
strNumTemp.charAt(intNumLen - i - 1) != '零') {
if (j == 1 && i < intNumLen) {
strNumChFormat = '拾' + strNumChFormat;
}
else if (j == 2 && i < intNumLen) {
strNumChFormat = '百' + strNumChFormat;
}
else if (j == 3 && i < intNumLen) {
strNumChFormat = '千' + strNumChFormat;
}
}
if (j == 4 && i < intNumLen) {
j = 0;
}
if (k == 4 && i < intNumLen) {
strNumChFormat = '万' + strNumChFormat;
}
else if (k == 8 && i < intNumLen) {
k = 0;
strNumChFormat = '亿' + strNumChFormat;
}
j++;
k++;
}
while (strNumChFormat.indexOf("位") != -1) {
strNumChFormat = strNumChFormat.replaceAll("位", " ");
}
if (strNumChFormat.substring(0, 2) == "一拾") {
strNumChFormat = strNumChFormat.substring(1, strNumChFormat.length());
}
if (strNumChFormat.indexOf("点") >= 0) {
String rebegin = strNumChFormat.substring(0,
strNumChFormat.indexOf("点"));
String relast = strNumChFormat.substring(strNumChFormat.indexOf("点"),
strNumChFormat.length());
for (int i = 1; i <= relast.length(); i++) {
relast = relast.replaceAll("拾", "");
relast = relast.replaceAll("百", "");
relast = relast.replaceAll("千", "");
relast = relast.replaceAll("万", "");
relast = relast.replaceAll("亿", "");
}
strNumChFormat = rebegin + relast;
}
}
catch (ArrayIndexOutOfBoundsException ex) {
ex.printStackTrace();
}
catch (Exception ex) {
ex.printStackTrace();
}
int off = strNumChFormat.indexOf("点");
strNumChFormat = strBegin + strNumChFormat.substring(0);
}
else {
strNumChFormat = "";
}
return strNumChFormat;
}
public static void main(String args[]) {
try {
String number = args[0].toString();
System.out.println("The number is: " + number);
Reader reader = new Reader(number);
System.out.println("Output String: " + reader.readNum());
}
catch (Exception ex) {
System.out.println("Please input like that: javac Reader <number>");
}
}
}


我这里正好有这个题的答案,但就是看不明白,头晕中。。。哪位大哥帮忙在里面注释一下呀
xyw7899 2007-12-03
  • 打赏
  • 举报
回复
我以前面试也碰到过类似的问题,我自己写了一个方法(保证没问题),不过在家里的电脑上,晚上发给你,留下你的邮箱啊。
NULLJAVA 2007-12-03
  • 打赏
  • 举报
回复
15楼的好强...保存研究...^^.
lionest 2007-12-03
  • 打赏
  • 举报
回复
佩服LZ的勇气!
AndyHun 2007-12-03
  • 打赏
  • 举报
回复
没时间具体做完,基本思路如下:

public class NuToCh{
String s[]="12345";
String unit=null;
for(int i=0;i<s.length;i++){
unit=Unit.getUnit(s);//调用下面的方法取得当前位的计数单位
//截取s的第一个字符,加上unit打印出来;此时s应该是2345

}
}

class Unit{
//下面根据长度,相应返回计数单位:s
public static String getUnit(String s){
int i=s.length;
case 1: .....
case 2: .....
.
.
.
}
}

按上面的方法应该可以了
jiangjinliang747 2007-12-03
  • 打赏
  • 举报
回复
值得学习学习...
黑夜愁客 2007-12-03
  • 打赏
  • 举报
回复
12楼答案不错,收了,研究一下...
xql80329 2007-12-03
  • 打赏
  • 举报
回复
打死不去华为!!!
加载更多回复(12)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧