將數(shù)字格式的計(jì)算結(jié)果轉(zhuǎn)為漢字格式
發(fā)表時(shí)間:2024-05-26 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]有沒有想過將數(shù)字格式的計(jì)算結(jié)果轉(zhuǎn)化為漢字格式的? 有人會(huì)問"干嘛要轉(zhuǎn), 數(shù)字形式不是蠻好嘛", 可是當(dāng)這個(gè)數(shù)字很長(zhǎng)的時(shí)候就不太容易讀出來了吧, 就算是有千分位的分隔符也不易順口說出, 因?yàn)檫@個(gè)符號(hào)是位英語(yǔ)行方便的, 不是適合我們的讀法. 那就自己寫一個(gè)函數(shù)來完成這項(xiàng)任務(wù)吧. 將...
有沒有想過將數(shù)字格式的計(jì)算結(jié)果轉(zhuǎn)化為漢字格式的? 有人會(huì)問"干嘛要轉(zhuǎn), 數(shù)字形式不是蠻好嘛", 可是當(dāng)這個(gè)數(shù)字很長(zhǎng)的時(shí)候就不太容易讀出來了吧, 就算是有千分位的分隔符也不易順口說出, 因?yàn)檫@個(gè)符號(hào)是位英語(yǔ)行方便的, 不是適合我們的讀法. 那就自己寫一個(gè)函數(shù)來完成這項(xiàng)任務(wù)吧.
將下列代碼加到你的網(wǎng)頁(yè)里, 通過num2chi()函數(shù)的調(diào)用就可以實(shí)現(xiàn)上述功能了, 快來試一試吧. 為了各位能讀懂這段代碼, 在下特意加入詳細(xì)注解, 還請(qǐng)老鳥們不要嫌羅唆, ^_^.
//----------------------FUNCTION BEGIN-----------------------------
//-------------------------------------------
//函數(shù)名: num2chi()
//叁 數(shù): 一數(shù)字
//返回值: 一字符串
//功 能: 將難讀的長(zhǎng)串?dāng)?shù)字轉(zhuǎn)為順口讀出的漢字
//作 者: chen.anson
//站 點(diǎn): HTTP://dreamer.oso.com.cn
//-------------------------------------------
function num2chi(result) {
var chiresult = ""; //定義返回值叁數(shù)chiresult為字符形式
result = result.toString(); //將result轉(zhuǎn)為字符形式
result = result.toLowerCase();
resultlen = result.length; //定義resultlen為result的長(zhǎng)度
tempresult = result; //定義中間變量tempresult
for (i=1;i<=resultlen;i++) //將字符串tempresult中的全部數(shù)字替換為漢字
{
tempresult = tempresult.replace("1","一");
tempresult = tempresult.replace("2","二");
tempresult = tempresult.replace("3","三");
tempresult = tempresult.replace("4","四");
tempresult = tempresult.replace("5","五");
tempresult = tempresult.replace("6","六");
tempresult = tempresult.replace("7","七");
tempresult = tempresult.replace("8","八");
tempresult = tempresult.replace("9","九");
tempresult = tempresult.replace("0","零");
tempresult = tempresult.replace(".","點(diǎn)");
tempresult = tempresult.replace("e+","冪");
}
while(tempresult.indexOf("零零")!=-1) //避免字符串tempresult中出現(xiàn)"零零", 但又不能改變字符串長(zhǎng)度
{
tempresult = tempresult.replace("零零","位零");
}
resultlen = tempresult.length; //再次確認(rèn)tempresult的長(zhǎng)度, 因"e+"->"冪"會(huì)引起長(zhǎng)度變化
for (i=1,j=1,k=1;i<=resultlen;i++) //開始轉(zhuǎn)換, i為位數(shù)確認(rèn)叁數(shù), j為"十百千"確認(rèn)叁數(shù), k為"萬(wàn)億"確認(rèn)叁數(shù)
{
//防止尾數(shù)為零, 如八拾零, 二拾零萬(wàn)
if (tempresult.charAt(resultlen-1)=="零"&&i==1)
chiresult = "位";
else if (tempresult.charAt(resultlen-i)=="零"&&j==1)
chiresult = "位" + chiresult;
//--------------------------------
//避免把"冪"和"點(diǎn)"當(dāng)做實(shí)際位數(shù), 而且單位確認(rèn)變量重新計(jì)數(shù)
else if (tempresult.charAt(resultlen-i)=="冪")
{
j=1;k=1;chiresult = tempresult.charAt(resultlen-i) + chiresult;continue;
}
else if (tempresult.charAt(resultlen-i)=="點(diǎn)")
{
j=1;k=1;chiresult = tempresult.charAt(resultlen-i) + chiresult;continue;
}
//--------------------------------------
else
chiresult = tempresult.charAt(resultlen-i) + chiresult;
//添加數(shù)字單位
if (tempresult.charAt(resultlen-i-1)!="位"&&tempresult.charAt(resultlen-i-1)!="零"&&tempresult.charAt(resultlen-i-1)!="冪")
{
if (j==1&&i<resultlen) chiresult = "拾" + chiresult;
else if (j==2&&i<resultlen) chiresult = "百" + chiresult;
else if (j==3&&i<resultlen) chiresult = "千" + chiresult;
}
if (j==4&&i<resultlen) j=0;
if (k==4&&i<resultlen&&tempresult.charAt(resultlen-i-1)!="冪") chiresult = "萬(wàn)" + chiresult;
else if (k==8&&i<resultlen&&tempresult.charAt(resultlen-i-1)!="冪") {k=0;chiresult = "億" + chiresult;}
//-----------
j++;k++;
}
while(chiresult.indexOf("位")!=-1) //避免字符串chiresult中出現(xiàn)"位"
{
chiresult = chiresult.replace("位","");
}
if (chiresult.substr(0,2)=="一拾") //避免出現(xiàn)"一拾二"等情況
chiresult = chiresult.substring(1,chiresult.length);
//冪和小數(shù)點(diǎn)後的數(shù)字應(yīng)直接讀出, 而沒有單位
if (chiresult.search("冪")>=0&&chiresult.search("點(diǎn)")>=0)
{
rebegin = chiresult.substring(0,chiresult.indexOf("點(diǎn)"));
relast = chiresult.substring(chiresult.indexOf("冪"),chiresult.length);
remid = chiresult.substring(chiresult.indexOf("點(diǎn)"),chiresult.indexOf("冪"));
for (i=1;i<=remid.length;i++)
{
remid = remid.replace("拾","");
remid = remid.replace("百","");
remid = remid.replace("千","");
remid = remid.replace("萬(wàn)","");
remid = remid.replace("億","");
}
chiresult = rebegin + remid + relast;
}
else if (chiresult.search("冪")<0&&chiresult.search("點(diǎn)")>=0)
{
rebegin = chiresult.substring(0,chiresult.indexOf("點(diǎn)"));
relast = chiresult.substring(chiresult.indexOf("點(diǎn)"),chiresult.length);
for (i=1;i<=relast.length;i++)
{
relast = relast.replace("拾","");
relast = relast.replace("百","");
relast = relast.replace("千","");
relast = relast.replace("萬(wàn)","");
relast = relast.replace("億","");
}
chiresult = rebegin + relast;
}
if (chiresult.search("冪")>=0) //將"冪"替換為"乘以拾的", 這樣可以直接讀出
{
chiresult = chiresult.replace("冪","乘以拾的");
chiresult = chiresult + "次方";
}
return chiresult;
}
//----------------------FUNCTION END-------------------------------
將下面這兩條語(yǔ)句放到script塊中試運(yùn)行一下看看結(jié)果對(duì)不對(duì), 另外可以訪問我的主頁(yè)http://dreamer.oso.com.cn 在休閑廣場(chǎng)里有一個(gè)彩票頁(yè)面, 就是用這段代碼實(shí)現(xiàn)的, 歡迎光臨.
hi='4648000567542450084.16415846E+766600050';
document.write(hi+"<br>"+num2chi(hi));