把數字轉成大寫的(一二三)
public static string ChineseTONumber(string chineseStr1)
{
string numStr = "0123456789";
string chineseStr = "零一二三四五六七八九";
char[] c = chineseStr1.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
int index = chineseStr.IndexOf(c[i]);
if (index != -1)
c[i] = numStr.ToCharArray()[index];
}
numStr = null;
chineseStr = null;
return new string(c);
}
public static string NumberToChinese(string numberStr)
{
string numStr = "0123456789";
string chineseStr = "零一二三四五六七八九";
char[] c = numberStr.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
int index = numStr.IndexOf(c[i]);
if (index != -1)
c[i] = chineseStr.ToCharArray()[index];
}
numStr = null;
chineseStr = null;
return new string(c);
} --------------------- 作者:sage425 來源:CSDN 原文:https://blog.csdn.net/sage425/article/details/64440588?utm_source=copy 版權宣告:本文為博主原創文章,轉載請附上博文連結!
上面的方法適合簡單的數字直接轉換情形
改進方法,適合0-99之內的數字轉換
public static string NumberToChinese(string numberStr)
{
string numStr = "0123456789";
string chineseStr = "零一二三四五六七八九";
char[] c = numberStr.ToCharArray();
string tempstr = "";
for (int i = 0; i < c.Length; i++)
{
int index = numStr.IndexOf(c[i]);
if (index != -1)
{
if (c[i].ToString() != "0" || i != c.Length - 1)
{
tempstr += chineseStr.ToCharArray()[index].ToString();
}
if (c.Length - i == 2)
{
tempstr += "十";
}
tempstr = tempstr.Replace("一十", "十");
}
}
numStr = null;
chineseStr = null;
return tempstr;
}
繼續擴充套件 一億之內的正整數,增加了非空驗證
public static string NumberToChinese(string numberStr)
{
if (numberStr == "")
{
return "0";
}
string numStr = "0123456789";
string chineseStr = "零一二三四五六七八九";
char[] c = numberStr.ToCharArray();
string tempstr = "";
for (int i = 0; i < c.Length; i++)
{
int index = numStr.IndexOf(c[i]);
if (index != -1)
{
if (c[i].ToString() != "0" || i != c.Length - 1)
{
tempstr += chineseStr.ToCharArray()[index].ToString();
}
if ((c.Length - i) % 4 == 1)
{
if ((c.Length - i) / 4 == 1)
{
tempstr += "萬";
}
}
if ((c.Length - i) % 4 == 0)
{
tempstr += "千";
}
if ((c.Length - i) % 4 == 3)
{
tempstr += "百";
}
if ((c.Length - i) % 4 == 2)
{
tempstr += "十";
}
tempstr = tempstr.Replace("零萬","萬").Replace("零千","零").Replace("零百","零").Replace("零十","零").Replace("零零","零");
}
}
if (c.Length % 4 == 2)
{
tempstr = tempstr.Replace("一十", "十");
}
if (tempstr.Substring(tempstr.Length - 1, 1) == "零")
{
tempstr = tempstr.Substring(0, tempstr.Length - 1);
}
numStr = null;
chineseStr = null;
return tempstr;
}