怎样将钱数转换成汉字?

yxxcrtd 2006-04-02 05:08:43
比如: 23.56 要转换成 二十三元零五十六分 元整

如何控制一个文本框中只能输入汉字?
...全文
347 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
boydream 2006-04-04
  • 打赏
  • 举报
回复
study~~~
lanfanghelanfanghe 2006-04-04
  • 打赏
  • 举报
回复
<script language="javascript">
//大写金额
function DX(n) {
var strOutput = "";
var strUnit = '千百拾亿千百拾万千百拾元角分';
n += "00";
var intPos = n.indexOf('.');
if (intPos >= 0)
n = n.substring(0, intPos) + n.substr(intPos + 1, 2);
strUnit = strUnit.substr(strUnit.length - n.length);
for (var i=0; i < n.length; i++)
strOutput += '零壹贰叁肆伍陆柒捌玖'.substr(n.substr(i,1),1) + strUnit.substr(i,1);
alert(strOutput);
return strOutput;
}

</script>

<input type="button" onclick="DX(1111111.11)">
「已注销」 2006-04-04
  • 打赏
  • 举报
回复
非常感谢 lydvqq(碧水情缘♀黑哥)

lydvqq 2006-04-03
  • 打赏
  • 举报
回复
<html>
<script>
String.prototype.chgChinese = function(s){
var s1=s.replace(/[^\u4e00-\u9fa5]/g,"");
return s1;
alert(s1);

}
function chgChinese(el){
var s = el.value.chgChinese(el.value);
alert(s);
el.value = s;

}

</script>
<body>
<input type=text onChange="chgChinese(this)">
</body>
</html>
图门的世界 2006-04-03
  • 打赏
  • 举报
回复
mark
zeq258 2006-04-03
  • 打赏
  • 举报
回复
学习下,收藏!

谢谢 lydvqq(碧水情缘♀黑哥)。
「已注销」 2006-04-03
  • 打赏
  • 举报
回复
Thanks: lydvqq(碧水情缘♀黑哥) !

关于 金额转换,我是想用在页面中反映,就是当用户输入一个数字之后,后面的一个文本框中会马上出项转换后的汉字...

这能用 javascript 实现吗?

lydvqq 2006-04-03
  • 打赏
  • 举报
回复
转个实例:
<script>
function regInput(reg)
{
var srcElem = event.srcElement
var oSel = document.selection.createRange()
oSel = oSel.duplicate()
oSel.text = ""
var srcRange = srcElem.createTextRange()
oSel.setEndPoint("StartToStart", srcRange)
var num = oSel.text + String.fromCharCode(event.keyCode) + srcRange.text.substr(oSel.text.length)
event.returnvalue = reg.test(num)
}

function chineseNumber(num)
{
if (isNaN(num) || num > Math.pow(10, 12)) return ""
var cn = "零壹贰叁肆伍陆柒捌玖"
var unit = new Array("拾佰仟", "分角")
var unit1= new Array("万亿", "")
var numArray = num.toString().split(".")
var start = new Array(numArray[0].length-1, 2)

function toChinese(num, index)
{
var num = num.replace(/\d/g, function ($1)
{
return cn.charAt($1)+unit[index].charAt(start--%4 ? start%4 : -1)
})
return num
}

for (var i=0; i<numArray.length; i++)
{
var tmp = ""
for (var j=0; j*4<numArray[i].length; j++)
{
var strIndex = numArray[i].length-(j+1)*4
var str = numArray[i].substring(strIndex, strIndex+4)
var start = i ? 2 : str.length-1
var tmp1 = toChinese(str, i)
tmp1 = tmp1.replace(/(零.)+/g, "零").replace(/零+$/, "")
tmp1 = tmp1.replace(/^壹拾/, "拾")
tmp = (tmp1+unit1[i].charAt(j-1)) + tmp
}
numArray[i] = tmp
}

numArray[1] = numArray[1] ? numArray[1] : ""
numArray[0] = numArray[0] ? numArray[0]+"圆" : numArray[0], numArray[1] = numArray[1].replace(/^零+/, "")
numArray[1] = numArray[1].match(/分/) ? numArray[1] : numArray[1]+"整"
return numArray[0]+numArray[1]
}

function aNumber(num)
{
var numArray = new Array()
var unit = "亿万圆$"
for (var i=0; i<unit.length; i++)
{
var re = eval("/"+ (numArray[i-1] ? unit.charAt(i-1) : "") +"(.*)"+unit.charAt(i)+"/")
if (num.match(re))
{
numArray[i] = num.match(re)[1].replace(/^拾/, "壹拾")
numArray[i] = numArray[i].replace(/[零壹贰叁肆伍陆柒捌玖]/g, function ($1)
{
return "零壹贰叁肆伍陆柒捌玖".indexOf($1)
})
numArray[i] = numArray[i].replace(/[分角拾佰仟]/g, function ($1)
{
return "*"+Math.pow(10, "分角 拾佰仟 ".indexOf($1)-2)+"+"
}).replace(/^\*|\+$/g, "").replace(/整/, "0")
numArray[i] = "(" + numArray[i] + ")*"+Math.ceil(Math.pow(10, (2-i)*4))
}
else numArray[i] = 0
}
return eval(numArray.join("+"))
}
</script>

<input id=up size=60 onkeypress="regInput(/^\d{0,12}(\.\d{0,2})?$/)"><button onclick="lw.value = chineseNumber(up.value)">转为大写</button><br>
<input id=lw size=60 value="壹仟壹佰壹拾壹圆整"><button onclick="up.value = aNumber(lw.value)">转为小写</button>
lydvqq 2006-04-03
  • 打赏
  • 举报
回复
另如果要做成js的,那你翻译下就OK了。
interpb 2006-04-02
  • 打赏
  • 举报
回复
收藏一下 呵呵

谢谢黑哥了

lydvqq 2006-04-02
  • 打赏
  • 举报
回复
上面是两个方法.请楼主参考下.
如何控制一个文本框中只能输入汉字?
如果是网页,那就用文本框的onChange()或onBlur()事件加上正则表达式来处理吧.
lydvqq 2006-04-02
  • 打赏
  • 举报
回复
import java.text.DecimalFormat;
public class ChangeMoney{

//我写的.
public static String moneyToRMB(double d){
String[] digit = {
"无", "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰",
"仟"};
String[] capi = {
"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
DecimalFormat df = new DecimalFormat("###0.00");
char[] c = df.format(d).toCharArray() ;
StringBuffer buf = new StringBuffer();
String reStr = null;
int cLength = c.length;

for(int i=0;i<c.length;i++){
String s = ""+c[i];
if(s.equals(".")){
cLength++;
continue;
}

int cp = Integer.parseInt(s);
buf.append(capi[ cp ]);
buf.append(digit[cLength-i-1]);
}

reStr = buf.toString();
System.out.println(":::::"+reStr);
for(int j=0;j<2;j++){
reStr = reStr.replaceAll( "零零", "零"); //replaceAll:jdk1.4才有
reStr = reStr.replaceAll( "零亿", "亿");
reStr = reStr.replaceAll( "零万", "万");
reStr = reStr.replaceAll( "零仟", "零");
reStr = reStr.replaceAll( "零佰", "零");
reStr = reStr.replaceAll( "零拾", "零");
reStr = reStr.replaceAll( "零角", "零");
reStr = reStr.replaceAll( "亿万", "亿零");
reStr = reStr.replaceAll( "零分", "元整");
reStr = reStr.replaceAll( "零元", "元");
reStr = reStr.replaceAll( "零零", "零");
reStr = reStr.replaceAll( "角元整", "角整");
reStr = reStr.replaceAll( "元元", "元");
}
return reStr;
}

//吴兵写的
/**
* 将数据转换为金额大写
* @param amount - 待转换的字符串
* @return 被转换后的金额大写。
*/
public static String money2RMB(double amount) {
String returnStr = "";
String[] strFigure = {
"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
double oldAmount = amount;
String[] strUnit = {
"无", "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰",
"仟"};

double tempAmount = amount;
amount = Math.abs(amount);
if (tempAmount != amount) {
returnStr = "负";

}
DecimalFormat df = new DecimalFormat("###0.00");
String strAmount = df.format(amount);
strAmount = replace(strAmount, ".", "");

int strLength = strAmount.length();
String myNum = "";
for (int i = 1; i <= strLength; i++) {
myNum = strAmount.substring(i - 1, i);
int len = strLength + 1 - i;
returnStr += strFigure[Integer.parseInt(myNum)] + strUnit[len];
}
//System.out.println(returnStr);

for (int i = 1; i < strUnit.length; i++) {
String tempStr = "";
if (! (strUnit[i].equals("万") || strUnit[i].equals("亿"))) {
tempStr = "零" + strUnit[i] + "零";
returnStr = replace(returnStr, tempStr, "零");
}
}

returnStr = replace(returnStr, "零亿", "亿");
returnStr = replace(returnStr, "零万", "万");
returnStr = replace(returnStr, "零仟", "零");
returnStr = replace(returnStr, "零佰", "零");
returnStr = replace(returnStr, "零拾", "零");
returnStr = replace(returnStr, "零角", "零");
returnStr = replace(returnStr, "亿万", "亿零");

if (returnStr.endsWith("零分")) {
returnStr = replace(returnStr, "零分", "元整");
}
returnStr = replace(returnStr, "元元", "元");
return returnStr;
}

/**
* 将line中所有的oldString用newString替换.
*
* @param line 提供将要替换的字符串
* @param oldString 被替换掉的字符串
* @param newString 替换成的字符串
*
* @return 所有的oldString用newString替换的line
*/
public static final String replace(String line, String oldString,
String newString) {
if (line == null) {
return null;
}
int i = 0;
if ( (i = line.indexOf(oldString, i)) >= 0) {
char[] line2 = line.toCharArray();
char[] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while ( (i = line.indexOf(oldString, i)) > 0) {
buf.append(line2, j, i - j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
return buf.toString();
}
return line;
}
public static void main(String[] args){
System.out.println(money2RMB(70000050002.100));
System.out.println(moneyToRMB(70000050002.100));
}
};

81,094

社区成员

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

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