日期:2014-05-20  浏览次数:20746 次

一个程序的编写
输入一个数字将其转化为大写,比如输入整型1034,得到“壹仟零叁拾肆”,
10034就是“壹万零叁拾肆”,怎么写?

------解决方案--------------------
给你吧,记得给分啊
public class Change{

void fun(String str)
{
String str1="零壹贰叁肆伍陆柒捌玖";
String str2="千百十万千百十元角分";
double money=Double.parseDouble(str);
long temp;
long m[] =new long[10];
int i;
boolean flag=false;
int x=0;
int y=9;
money=money*100+0.5;
temp=(long)money;
m[0]=temp/1000000000;
m[1]=(temp%1000000000)/100000000;
m[2]=(temp%100000000)/10000000;
m[3]=(temp%10000000)/1000000;
m[4]=(temp%1000000)/100000;
m[5]=(temp%100000)/10000;
m[6]=(temp%10000)/1000;
m[7]=(temp%1000)/100;
m[8]=(temp%100)/10;
m[9]=temp%10;
for(i=0;i<10;i++)
{
if(m[i]!=0)
{
x=i;
break;
}
}
for(i=9;i>=0;i--)
{
if(m[i]!=0)
{
y=i;
break;
}
}
for(i=x;i<y+1;i++)
{
//System.out.print(m[i]);
if(m[i]==0)
{
if(flag==false)
{
System.out.print(""+str1.charAt((int)m[i]));
flag=true;
}

}
else
{

System.out.print(""+str1.charAt((int)m[i])+str2.charAt(i));
flag=false;
}
}
}
public static void main(String args[])
{
Change obj=new Change();
obj.fun(args[0]);
}

}

------解决方案--------------------
又是人民币...

Java code
import java.text.*;
public class ChineseCurrency{
    public static void main(String[] args) {
        double number = 100035.53;    
        System.out.println(toChineseCurrency(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();
            StringBuilder sb = new StringBuilder(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();
        }
    }
}

------解决方案--------------------
照楼主帖子的意思,也写了完整的一个,供大家参考

import java.util.HashMap;

public class NumberToChinese {
  
public static void main(String[] args)
{
StringBuffer numberToCn = new StringBuffer();

String number = "-2813040410.0787003560";

HashMap cnHash = getCNHash();

HashMap unitHash = getUnitHash();

String[] partOfNumber = null;

if(number.substring(0, 1).equals("-")) //判断数字是否为负数,并将数划分为小数、整数两部分
{
numberToCn.append("負");
partOfNumber = number.substring(1).split("\\.");
}
else
partOfNumber = number.substring(0).split("\\.");


if(partOfNumber.length == 1) //如果输入的数没有小数则只进行整数部分转化
{
String str = PositiveChangeToCN(partOfNumber[0],cnHash,unitHash);