.net页面实现数字实时转换大写金额
在建设网上银行转账的时候,页面可以实现动态的显示大写金额,而页面上的textchanged事件又不像cs里面那么灵敏,所以要:
1.新建一个asmx文件Wpservices.asmx
2、文件内容为
[System.Web.Script.Services.ScriptService]
public  class Wpservices : System.Web.Services.WebService {
public Wpservices () {
        //如果使用设计的组件,请取消注释以下行 
        //InitializeComponent(); 
    }
    [WebMethod]
    public string HelloWorld(string text)
    {
        
        return "hello word";
    }
    [WebMethod]
    public string Clearz(string str)
    {
        System.Text.RegularExpressions.Regex regex = new Regex(@"零{1,}", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
string strs = regex.Replace(str, "零");
        if (strs.Length > 0)
        {
            //检查最字符串的结尾
            System.Text.RegularExpressions.Regex Regtex = new Regex(@"$", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            return Regtex.Replace(strs, "");
        }
        return strs;
    }
    [WebMethod]
    public string Switch(int i)
    {
        string ReturnCh = string.Empty;
        switch (i % 8)
        {
            case 1:
                ReturnCh = "拾";
                break;
            case 2:
                ReturnCh = "佰";
                break;
            case 3:
                ReturnCh = "仟";
                break;
            case 4:
                ReturnCh = "万";
                break;
            case 5:
                ReturnCh = "拾";
                break;
            case 6:
                ReturnCh = "佰";
                break;
            case 7:
                ReturnCh = "仟";
                break;
            case 0:
                ReturnCh = "亿";
                break;
            default:
                break;
        }
        return ReturnCh;
    }
    [WebMethod]
    public string Switch(char ch)
    {
        string ReturnCh = string.Empty;
        switch (ch)
        {
            case '0':
                ReturnCh = "零";
                break;
            case '1':
                ReturnCh = "壹";
                break;
            case '2':
                ReturnCh = "贰";
                break;
            case '3':
                ReturnCh = "叁";
                break;
            case '4':
                ReturnCh = "肆";
                break;
            case '5':
                ReturnCh = "伍";
                break;
            case '6':
                ReturnCh = "陆";
                break;
            case '7':
                ReturnCh = "柒";
                break;
            case '8':
                ReturnCh = "捌";
                break;
            case '9':
                ReturnCh = "玖";
                break;
            case '.':
                ReturnCh = "元";
                break;
            default:
                break;
        }
        return ReturnCh;
    }
    [WebMethod]
    public   string getMoney(string text)
    {
        //  text = float.Parse(text).ToString();
        string[] m = text.Split('.');
        // TextBox3.Text = m[0];
        char[] Num = m[0].ToCharArray();
        Array.Reverse(Num);
        int length = Num.Length;
        string[] Result = new string[Num.Length];
        for (int i = 0; i < length; i++)
        {
            if (i == 0)
            {
                if (Switch(Num[i]) == "零")
                {
                    continue;
                }
                else
                {
                    Result[i] = Switch(Num[i]);
                }
            }
            else
            {
                if (Switch(Num[i]) == "零" && i < 4)
                {
                    Result[i] = "零";
                }
                else if (Switch(Num[i]) == "零" && i == 4)//万
                {
                    Result[i] = Switch(i);
                }
                else if (Switch(Num[i]) == "零" && i == 8)//亿
                {
                    Result[i] = Switch(i) + "亿";
                }
                else if (Switch(Num[i]) == "零")
                {
                    Result[i] = "零";
                }
                else if (Switch(Num[i]) != "零")
                {
                    Result[i] = Switch(Num[i]) + Switch(i);
                }
}
        }
        Array.Reverse(Result);
        string allResult = string.Empty;
        foreach (string a in Result)
        {
            allResult += a;
        }
        //allResult = allResult + "yuan";
        if (!allResult.EndsWith("元"))
        {
            allResult = allResult + "元";
        }
        if (m.Length == 2)
        {
            string mon = m[1];
            string h = "";
            char[] Num0 = m[1].ToCharArray();
            if (m[1].Length == 1)
            {
                m[1] = m[1] + "0";
            }
            if (Num0.Length >= 2)
            {
                for (int j = 0; j < 2; j++)
                {
                    h = Switch(Num0[j]);
                    if (j == 0 && h != "零")
                    {
                        allResult = allResult + h + "角";
                    }
                    if (j == 1 && h != "零")
                    {
                        allResult = allResult + h + "分";
                    }
                }
            }
        }
        allResult = allResult.Trim();
        allResult = allResult.Replace("零零零零零", "零");
        allResult = allResult.Replace("零零零零", "零");
        allResult = allResult.Replace("零零零", "零");
        allResult = allResult.Replace("零零", "零");
        allResult = allResult.Replace("亿万", "亿");
        allResult = allResult.Replace("亿亿", "亿");
        allResult = allResult.Replace("零万", "万");
        allResult = allResult.Replace("零亿", "亿");
        allResult = allResult.Replace("零亿", "亿");
        if (allResult.Length > 1 && allResult.Substring(allResult.Length - 1, 1) == "零")
        {
            allResult = allResult.Remove(allResult.Length - 1, 1);
        }
        if (m.Length == 2)
        {
        }
        else
        {
            allResult = allResult + "元";
        }
        allResult = allResult.Replace("零万", "零");
        allResult = allResult.Replace("零亿", "零");
        allResult = allResult.Replace("元元", "元");
        allResult = allResult.Replace("零元", "元");
        allResult = allResult.Replace("元元", "元");
        return allResult;
    }
    3、在aspx页面上调用
    <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Services>
                <asp:ServiceReference Path="~/Wpservices.asmx" />
            </Services>
    </asp:ScriptManager>
    <script type="text/javascript">
    function demo()
    {
    Wpservices.getMoney(document.getElementById("Text1").value,onCallBackSuccessed,onCallBackfailed,"");
    
    }
    function onCallBackSuccessed(result)
     {
        document.getElementById("Text2").value=result;
     }
     function onCallBackfailed(result)
     {
        document.getElementById("Text2").value=result;
     }
    </script>
 
                    
                     
                    
                 
                    
                 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号