最近在做一个项目,需要把中文的数字转换为阿拉伯数字,再网上查找了一下,只有阿拉伯数字转换为中文数字,没有我需要的,因此自己写了一个,放到这里记录一下, 并且请大家帮忙测试下.
/// <summary> /// 中文数字转换为阿拉伯数字 /// </summary> /// <param name="value"></param> /// <returns></returns> public static int Chinese2Number(string value) { string[] unit = new string[] { "十", "百", "千", "万" }; Dictionary<string, string> m_Dict = new Dictionary<string, string>(); m_Dict.Add("零", "0"); m_Dict.Add("一", "1"); m_Dict.Add("二", "2"); m_Dict.Add("三", "3"); m_Dict.Add("四", "4"); m_Dict.Add("五", "5"); m_Dict.Add("六", "6"); m_Dict.Add("七", "7"); m_Dict.Add("八", "8"); m_Dict.Add("九", "9"); int resut = 0; string resulttemp = ""; foreach (var item in value) { string temp = item.ToString(); if (m_Dict.ContainsKey(temp)) { resulttemp += m_Dict[temp]; } else { if (resulttemp == "") resulttemp = "0"; switch (temp) { case "十": resut = resut + Convert.ToInt32(resulttemp) * 10; break; case "百": resut = resut + Convert.ToInt32(resulttemp) * 100; break; case "千": resut = resut + Convert.ToInt32(resulttemp) * 1000; break; case "万": resut = resut * 10000 + Convert.ToInt32(resulttemp) * 10000; break; default: break; } resulttemp = ""; } } if (resulttemp != "") resut = resut + Convert.ToInt32(resulttemp); return resut; } }
请大家多提提意见。