C#版本
View Code
using System; using System.Collections.Generic; using System.Text; namespace NumberConventer { class Class1 { private string[] CnNum = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" }; private string[] ShiNum = { "千", "", "十", "百" }; private string[] WanNum = { "", "万", "亿" }; private string NumTh, OutCn; private bool isNumber(string aaa) { try { int bbb = Convert.ToInt16(aaa); return true; } catch { return false; } }//判断是否为数字 private string ChangeChineseNum(string Number,int Numlan) { bool Zero = false, Wan = false, Hand = true; //zero 为是否需要输出汉字“零”,Wan为首否需要输出汉字“万”或“亿”,Hand 判断是否为第一个非零数字。 int NumTemp; bool Booltemp = Int32.TryParse(Number, out NumTemp); if (!Booltemp) { NumTemp = 0; } for (int i = 1; i <= Numlan; i++) { NumTh = Number.Substring(i - 1, 1); int NowNum, NowTh = Numlan + 1 - i; if (isNumber(NumTh)) { NowNum = Convert.ToInt16(NumTh); //OutCn = OutCn + CnNum[i]; } else { break; } if (NowNum == 0) { if (Hand == true) { continue; } else { Zero = true; } } else { Wan = true; if (Hand == true) { if (NumTemp > 9 && NumTemp < 20) { OutCn = OutCn + ShiNum[NowTh % 4]; } else { OutCn = OutCn + CnNum[NowNum] + ShiNum[NowTh % 4]; } Hand = false; } else { if (Zero == false) { OutCn = OutCn + CnNum[NowNum] + ShiNum[NowTh % 4]; } else { OutCn = OutCn + CnNum[0] + CnNum[NowNum] + ShiNum[NowTh % 4]; Zero = false; } } } if (Wan == true && (NowTh - 1) % 4 == 0) { OutCn = OutCn + WanNum[((NowTh - 1) / 4) % 2]; for (int j = (NowTh - 1) / 8; j > 0; j = j - 1) { OutCn = OutCn + WanNum[2]; } Wan = false; } } if (isNumber(NumTh)) { return OutCn; } else { return "包含非阿拉伯字符!请更正!"; } } } }
javascript版本
View Code
function ChangeChineseNum(Number) { var isNumber = function (aaa) { try { parseInt(aaa); return true; } catch (err) { return false; } }; var CnNum = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"]; var ShiNum = ["千", "", "十", "百"]; var WanNum = ["", "万", "亿"]; var NumTh, OutCn = ""; var Zero = false, Wan = false, Hand = true; //zero 为是否需要输出汉字“零”,Wan为首否需要输出汉字“万”或“亿”,Hand 判断是否为第一个非零数字。 var Numlan; var NumTemp; var Booltemp; if (isNumber(Number)) { NumTemp = parseInt(Number); Numlan = NumTemp.toString().length; } else { NumTemp = 0; Numlan = 0; } for (var i = 1; i <= Numlan; i++) { NumTh = Number.toString().substring(i - 1, i); var NowNum, NowTh = Numlan + 1 - i; if (isNumber(NumTh)) { NowNum = parseInt(NumTh); //OutCn = OutCn + CnNum[i]; } else { break; } if (NowNum == 0) { if (Hand == true) { continue; } else { Zero = true; } } else { Wan = true; if (Hand == true) { if (NumTemp > 9 && NumTemp < 20) { OutCn = OutCn + ShiNum[NowTh % 4]; } else { OutCn = OutCn + CnNum[NowNum] + ShiNum[NowTh % 4]; } Hand = false; } else { if (Zero == false) { OutCn = OutCn + CnNum[NowNum] + ShiNum[NowTh % 4]; } else { OutCn = OutCn + CnNum[0] + CnNum[NowNum] + ShiNum[NowTh % 4]; Zero = false; } } } if (Wan == true && (NowTh - 1) % 4 == 0) { OutCn = OutCn + WanNum[((NowTh - 1) / 4) % 2]; for (var j = parseInt((NowTh - 1) / 8); j > 0; j = j - 1) { OutCn = OutCn + WanNum[2]; } Wan = false; } } if (isNumber(NumTh)) { return OutCn; } else { return "包含非阿拉伯字符!请更正!"; } }