当前位置: 代码迷 >> J2SE >> 人民币数字转化为文字,如1235如何输出一千二百三十五元
  详细解决方案

人民币数字转化为文字,如1235如何输出一千二百三十五元

热度:236   发布时间:2016-04-24 18:01:03.0
人民币数字转化为文字,如1235怎么输出一千二百三十五元?
感觉很简单   但是没思路   望高人指教下。。

------解决方案--------------------
以前曾有人发过~

import java.awt.*;
import java.awt.event.*;

class Test
{
public static void main(String[] args)
{
String num = "零壹贰叁肆伍陆柒捌玖 ";
String dw = "圆拾佰仟万亿 ";
String m = "30020.23 ";
String mm[] = null;
mm = m.split( "\\. ");
String money = mm[0];

String result = num.charAt(Integer.parseInt( " " + mm[1].charAt(0))) +
"角 " +
num.charAt(Integer.parseInt( " " + mm[1].charAt(1))) + "分 ";

for (int i = 0; i < money.length(); i++)
{
String str = " ";
int n = Integer.parseInt(money.substring(money.length() - i - 1,
money.length() - i));
str = str + num.charAt(n);
if (i == 0)
{
str = str + dw.charAt(i);
}
else if ( (i + 4) % 8 == 0)
{
str = str + dw.charAt(4);
}
else if (i % 8 == 0)
{
str = str + dw.charAt(5);
}
else
{
str = str + dw.charAt(i % 4);
}
result = str + result;
}
result = result.replaceAll( "零([^圆]{1}) ", "零 ");
result = result.replaceAll( "零+ ", "零 ");
result = result.replaceAll( "零圆 ", "圆 ");
System.out.println(result);

}
}

------解决方案--------------------
<script language= "javascript ">
var strArray = new Array();
strArray[0] = "元 "
strArray[1] = "十 "
strArray[2] = "百 "
strArray[3] = "千 "

var str = "1235 ";

var len = str.length;

var ss = " ";
for(i=1;i <=len;i++){
ss += str.substring(i-1,i)+strArray[len-i];
}
alert(ss);
</script>
------解决方案--------------------
1.(C#):
//加到类的定义部分
private static string[] cstr={ "零 ", "壹 ", "贰 ", "叁 ", "肆 ", "伍 ", "陆 ", "柒 ", "捌 ", "玖 "};
private static string[] wstr={ " ", " ", "拾 ", "佰 ", "仟 ", "萬 ", "拾 ", "佰 ", "仟 ", "億 ", "拾 ", "佰 ", "仟 "};
//数字必须在12位整数以内的字符串
//调用方式如:Label1.Text=ConvertInt( "数字字符串 ");

public string ConvertInt(string str)
{
int len=str.Length;
int i;
string tmpstr,rstr;
rstr= " ";
for(i=1;i <=len;i++)
{
tmpstr=str.Substring(len-i,1);
rstr=string.Concat(cstr[Int32.Parse(tmpstr)]+wstr[i],rstr);
}
rstr=rstr.Replace( "拾零 ", "拾 ");
rstr=rstr.Replace( "零拾 ", "零 ");
rstr=rstr.Replace( "零佰 ", "零 ");
rstr=rstr.Replace( "零仟 ", "零 ");
rstr=rstr.Replace( "零萬 ", "萬 ");
for(i=1;i <=6;i++)
rstr=rstr.Replace( "零零 ", "零 ");
rstr=rstr.Replace( "零萬 ", "零 ");