Home > php教程 > php手册 > 将数字格式的计算结果转为汉字格式_php基础

将数字格式的计算结果转为汉字格式_php基础

WBOY
Release: 2016-05-17 09:08:51
Original
1097 people have browsed it

有没有想过将数字格式的计算结果转化为汉字格式的? 有人会问"干嘛要转, 数字形式不是蛮好嘛", 可是当这个数字很长的时候就不太容易读出来了吧, 就算是有千分位的分隔符也不易顺口说出, 因为这个符号是位英语行方便的, 不是适合我们的读法. 那就自己写一个函数来完成这项任务吧.

将下列代码加到你的网页里, 通过num2chi()函数的调用就可以实现上述功能了, 快来试一试吧. 为了各位能读懂这段代码, 在下特意加入详细注解, 还请老鸟们不要嫌罗唆, ^_^.

//----------------------FUNCTION BEGIN-----------------------------
//-------------------------------------------
//函数名: num2chi()
//叁  数: 一数字
//返回值: 一字符串
//功  能: 将难读的长串数字转为顺口读出的汉字
//作  者: chen.anson  
//站  点: HTTP://dreamer.oso.com.cn
//-------------------------------------------

function num2chi(result) {

var chiresult = "";            //定义返回值叁数chiresult为字符形式
result = result.toString();    //将result转为字符形式
result = result.toLowerCase();
resultlen = result.length;    //定义resultlen为result的长度
tempresult = result;        //定义中间变量tempresult

for (i=1;i{
    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(".","点");
    tempresult = tempresult.replace("e+","幂");
}

while(tempresult.indexOf("零零")!=-1)    //避免字符串tempresult中出现"零零", 但又不能改变字符串长度
{
    tempresult = tempresult.replace("零零","位零");
}

resultlen = tempresult.length;    //再次确认tempresult的长度, 因"e+"->"幂"会引起长度变化

for (i=1,j=1,k=1;i{
    //防止尾数为零, 如八拾零, 二拾零万
    if (tempresult.charAt(resultlen-1)=="零"&&i==1)
        chiresult = "位";
    else if (tempresult.charAt(resultlen-i)=="零"&&j==1)
        chiresult = "位" + chiresult;
    //--------------------------------

    //避免把"幂"和"点"当做实际位数, 而且单位确认变量重新计数
    else if (tempresult.charAt(resultlen-i)=="幂")
    {
        j=1;k=1;chiresult = tempresult.charAt(resultlen-i) + chiresult;continue;
    }
    else if (tempresult.charAt(resultlen-i)=="点")
    {
        j=1;k=1;chiresult = tempresult.charAt(resultlen-i) + chiresult;continue;
    }
    //--------------------------------------
    else
    chiresult = tempresult.charAt(resultlen-i) + chiresult;
    //添加数字单位
        if (tempresult.charAt(resultlen-i-1)!="位"&&tempresult.charAt(resultlen-i-1)!="零"&&tempresult.charAt(resultlen-i-1)!="幂")
        {
            if (j==1&&i            else if (j==2&&i            else if (j==3&&i        }
        if (j==4&&i        if (k==4&&i        else if (k==8&&i    //-----------
    j++;k++;
}

while(chiresult.indexOf("位")!=-1)    //避免字符串chiresult中出现"位"
{
    chiresult = chiresult.replace("位","");
}

if (chiresult.substr(0,2)=="一拾")    //避免出现"一拾二"等情况
chiresult = chiresult.substring(1,chiresult.length);

//幂和小数点後的数字应直接读出, 而没有单位
if (chiresult.search("幂")>=0&&chiresult.search("点")>=0)
{
    rebegin = chiresult.substring(0,chiresult.indexOf("点"));
    relast = chiresult.substring(chiresult.indexOf("幂"),chiresult.length);
    remid = chiresult.substring(chiresult.indexOf("点"),chiresult.indexOf("幂"));
    for (i=1;i    {
        remid = remid.replace("拾","");
        remid = remid.replace("百","");
        remid = remid.replace("千","");
        remid = remid.replace("万","");
        remid = remid.replace("亿","");
    }
    chiresult = rebegin + remid + relast;
}
else if (chiresult.search("幂")=0)
{
    rebegin = chiresult.substring(0,chiresult.indexOf("点"));
    relast = chiresult.substring(chiresult.indexOf("点"),chiresult.length);
    for (i=1;i    {
        relast = relast.replace("拾","");
        relast = relast.replace("百","");
        relast = relast.replace("千","");
        relast = relast.replace("万","");
        relast = relast.replace("亿","");
    }
    chiresult = rebegin + relast;
}

if (chiresult.search("幂")>=0)    //将"幂"替换为"乘以拾的", 这样可以直接读出  
{
    chiresult = chiresult.replace("幂","乘以拾的");
    chiresult = chiresult + "次方";
}
return chiresult;
}

//----------------------FUNCTION END-------------------------------


将下面这两条语句放到script块中试运行一下看看结果对不对, 另外可以访问我的主页http://dreamer.oso.com.cn 在休闲广场里有一个彩票页面, 就是用这段代码实现的, 欢迎光临.
hi='4648000567542450084.16415846E+766600050';
document.write(hi+"
"+num2chi(hi));

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template