数字转中文问题

hudenq 2011-10-11 08:29:38
string strChnNames="零壹贰叁肆伍陆柒捌玖";
string strNumNames="0123456789";

要求输出4位以内 格式 如3001 》》 叁零零壹, 12 》》拾贰
...全文
190 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hudenq 2011-10-11
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hxl20081219 的回复:]
12 》》?壹贰?
[/Quote]
只有两种就可以了,4位数和两位数
是,如果是4位数,不用其他,直接一对一转换,2位数也可以这样如12 壹拾贰 23要贰拾叁
sunyaxingqq 2011-10-11
  • 打赏
  • 举报
回复
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public const string STR_UPPER="零壹贰叁肆伍陆柒捌玖";
public const string STR_UNIT="分角元拾佰仟萬拾佰仟亿";
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text.Trim()))
{
string strcapResult = "";
string strCapValue = string.Format("{0:F2}",Convert .ToDouble (textBox1 .Text ));
int intDotPos = strCapValue.IndexOf(".");
int intBeginPos = 0;
string strCapint = strCapValue.Substring(intBeginPos ,intDotPos );
string strCapDec = strCapValue.Substring(intDotPos+1);
strcapResult = convertinttoupper(strCapint) + convertdectoupper(strCapDec,Convert .ToInt32 (strCapDec )>=1?true :false );
if (Convert.ToDouble(strCapDec) == 0)
{
strcapResult += "整";
}
textBox2.Text = strcapResult;
}

}

private void Form1_Load(object sender, EventArgs e)
{

}
//小数点前的转换
private string convertinttoupper(string strValue)
{
string strCurrCap = "";
string strCapResult = "";
int intPrevChar = -1;
int intCurrChar = 0;
int intPosIndex = 2;
if (Convert.ToDouble(strValue) == 0)
return "";
for (int i = strValue.Length-1; i >= 0; i--)
{
intCurrChar = Convert.ToInt32(strValue .Substring (i,1));
if (intCurrChar != 0)
{
strCurrCap = STR_UPPER.Substring(intCurrChar, 1) + STR_UNIT.Substring (intPosIndex, 1);
}
else
{
switch (intPosIndex)
{
case 2:
strCurrCap = "元";
break;
case 6:
strCurrCap = "萬";
break;
case 10:
strCurrCap = "亿";
break;
}
if (intPrevChar != 0)
{
if (strCurrCap != "")
{
if (strCurrCap != "元")
{
strCurrCap += "零";
}

}
else
{
strCurrCap = "零";
}
}
}
strCapResult = strCurrCap + strCapResult;
intPrevChar = intCurrChar;
intPosIndex += 1;
strCurrCap = "";
}
return strCapResult;
}
private string convertdectoupper(string strCapValue,bool isExistint)
{
string strCurrCap = "";
string strCapResult = "";
int intCurrChar = 0;
int intPosIndex = 1;
if (Convert.ToDouble(strCapValue) == 0)
return "";
for (int i = 0; i < strCapValue.Length; i++)
{
intCurrChar = Convert.ToInt32(strCapValue .Substring (i,1));
if (intCurrChar == 0)
{
if (i == 0)
{
if (isExistint)
{
strCurrCap = "零";
}
}
}
else
{
strCurrCap = STR_UPPER.Substring(intCurrChar ,1)+STR_UNIT .Substring (intPosIndex ,1);
}
strCapResult += strCurrCap;
intPosIndex -= 1;
strCurrCap = "";
}
return strCapResult;

}
}



好东西大家要一起分享
hudenq 2011-10-11
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 q107770540 的回复:]
C# code


using System;
using System.Text.RegularExpressions;

class Program
{
// 把阿拉伯数字的金额转换为中文大写数字
static string ConvertToChinese(double x)
{
string s = x.ToString("#L#E#D#C#K#E……
[/Quote]
谢谢了,但我要求只是数字中文一对一转换,不是人民币
Ny-6000 2011-10-11
  • 打赏
  • 举报
回复
楼上的.好.
q107770540 2011-10-11
  • 打赏
  • 举报
回复

using System;
using System.Text.RegularExpressions;

class Program
{
// 把阿拉伯数字的金额转换为中文大写数字
static string ConvertToChinese(double x)
{
string s = x.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
return Regex.Replace(d, ".", delegate(Match m) { return "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟萬億兆京垓秭穰"[m.Value[0] - '-'].ToString(); });
}

static void Main()
{
Random r = new Random();
for (int i = 0; i < 10; i++)
{
double x = r.Next() / 100.0;
Console.WriteLine("{0,14:N2}: {1}", x, ConvertToChinese(x));
}
}
}
/* 可能的输出:
5,607,400.68: 伍佰陆拾萬柒仟肆佰元陆角捌分
2,017,723.33: 贰佰零壹萬柒仟柒佰贰拾叁元叁角叁分
751,181.17: 柒拾伍萬壹仟壹佰捌拾壹元壹角柒分
7,849,851.53: 柒佰捌拾肆萬玖仟捌佰伍拾壹元伍角叁分
2,629,143.90: 贰佰陆拾贰萬玖仟壹佰肆拾叁元玖角
13,461,629.68: 壹仟叁佰肆拾陆萬壹仟陆佰贰拾玖元陆角捌分
4,594,391.16: 肆佰伍拾玖萬肆仟叁佰玖拾壹元壹角陆分
13,046,560.60: 壹仟叁佰零肆萬陆仟伍佰陆拾元陆角
13,041,371.21: 壹仟叁佰零肆萬壹仟叁佰柒拾壹元贰角壹分
20,639,609.44: 贰仟零陆拾叁萬玖仟陆佰零玖元肆角肆分
*/

夏天的鱼 2011-10-11
  • 打赏
  • 举报
回复
12 》》拾贰?壹贰?
天二天 2011-10-11
  • 打赏
  • 举报
回复
如3001 》》 叁零零壹, 12 》》拾贰 这两个例子有所不同,楼主发现没有?

110,549

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧