如何用中文读出阿拉伯数字?
比如:9090中文:九千零九拾 --------------------编程问答-------------------- look --------------------编程问答-------------------- 这不是我当年上大一的作业嘛 --------------------编程问答-------------------- Text-to-Speech Engine应该可以直接读吧 --------------------编程问答-------------------- 以前做过js版的 --------------------编程问答--------------------
对的,可以用TTS来实现的 --------------------编程问答-------------------- http://blog.csdn.net/fanz2000/archive/2004/08/14/74587.aspx
这个行 --------------------编程问答-------------------- 笨办法!
判断数字位数,取每一位的位数,拿出来转换成汉字,拼字符串,重载位数。
见笑~ --------------------编程问答-------------------- TTS --------------------编程问答-------------------- 这个应该是
using System;--------------------编程问答-------------------- 牛! --------------------编程问答-------------------- 我做了个,比较粗糙,但是十位数以内还是没问题的。代码如下:
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
namespace MedSystem
{
class ProcessDate
{
// SqlConnection Conn = new SqlConnection(ConfigurationSettings.AppSettings["ConStr"].ToString());
//大小写转换
public static string CmycurD(decimal num)
{
string str1 = "零壹贰叁肆伍陆柒捌玖"; //0-9所对应的汉字
string str2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; //数字位所对应的汉字
string str3 = ""; //从原num值中取出的值
string str4 = ""; //数字的字符串形式
string str5 = ""; //人民币大写金额形式
int i; //循环变量
int j; //num的值乘以100的字符串长度
string ch1 = ""; //数字的汉语读法
string ch2 = ""; //数字位的汉字读法
int nzero = 0; //用来计算连续的零值是几个
int temp; //从原num值中取出的值
num = Math.Round(Math.Abs(num), 2); //将num取绝对值并四舍五入取2位小数
str4 = ((long)(num * 100)).ToString(); //将num乘100并转换成字符串形式
j = str4.Length; //找出最高位
if (j > 15) { return "溢出"; }
str2 = str2.Substring(15 - j); //取出对应位数的str2的值。如:200.55,j为5所以str2=佰拾元角分
//循环取出每一位需要转换的值
for (i = 0; i < j; i++)
{
str3 = str4.Substring(i, 1); //取出需转换的某一位的值
temp = Convert.ToInt32(str3); //转换为数字
if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15))
{
//当所取位数不为元、万、亿、万亿上的数字时
if (str3 == "0")
{
ch1 = "";
ch2 = "";
nzero = nzero + 1;
}
else
{
if (str3 != "0" && nzero != 0)
{
ch1 = "零" + str1.Substring(temp * 1, 1);
ch2 = str2.Substring(i, 1);
nzero = 0;
}
else
{
ch1 = str1.Substring(temp * 1, 1);
ch2 = str2.Substring(i, 1);
nzero = 0;
}
}
}
else
{
//该位是万亿,亿,万,元位等关键位
if (str3 != "0" && nzero != 0)
{
ch1 = "零" + str1.Substring(temp * 1, 1);
ch2 = str2.Substring(i, 1);
nzero = 0;
}
else
{
if (str3 != "0" && nzero == 0)
{
ch1 = str1.Substring(temp * 1, 1);
ch2 = str2.Substring(i, 1);
nzero = 0;
}
else
{
if (str3 == "0" && nzero >= 3)
{
ch1 = "";
ch2 = "";
nzero = nzero + 1;
}
else
{
if (j >= 11)
{
ch1 = "";
nzero = nzero + 1;
}
else
{
ch1 = "";
ch2 = str2.Substring(i, 1);
nzero = nzero + 1;
}
}
}
}
}
if (i == (j - 11) || i == (j - 3))
{
//如果该位是亿位或元位,则必须写上
ch2 = str2.Substring(i, 1);
}
str5 = str5 + ch1 + ch2;
if (i == j - 1 && str3 == "0")
{
//最后一位(分)为0时,加上“整”
str5 = str5 + '整';
}
}
if (num == 0)
{
str5 = "零元整";
}
return str5;
}
/**/
/// <summary>
/// 一个重载,将字符串先转换成数字在调用CmycurD(decimal num)
/// </summary>
/// <param name="num">用户输入的金额,字符串形式未转成decimal</param>
/// <returns></returns>
public static string CmycurD(string numstr)
{
try
{
decimal num = Convert.ToDecimal(numstr);
return CmycurD(num);
}
catch
{
return "非数字形式!";
}
}
}
}
# define maxsize 10
# include <stdio.h>
void main()
{
int i,j,a,b,d,k;
char c[maxsize]={'0'};
printf("请输入十位以内的阿拉伯数字:\n");
for(i=0;((c[i]=getchar())!='\n')&&(i<=9);++i) printf("%c",c[i]);
printf("\n\n");
b=i;
for(j=0;(j<=b)&&(j<=9);j++)
{
a=(i-1)%4;
--i;
k=c[j]-48;
switch(k)
{
case 0:
if(a&&k||(i%4==3)||(i%4==2))
printf("零");
break;
case 1:
printf("壹");
break;
case 2:
printf("贰");
break;
case 3:
printf("叁");
break;
case 4:
printf("肆");
break;
case 5:
printf("伍");
break;
case 6:
printf("陆");
break;
case 7:
printf("柒");
break;
case 8:
printf("捌");
break;
case 9:
printf("玖");
break;
}
if(k)switch(a)
{
case 1:
printf("十");
break;
case 2:
printf("百");
break;
case 3:
printf("千");
break;
}
if(!a)
{
d=i/4;
switch(d){
case 1:
printf("万");
break;
case 2:
printf("亿");
break;
}
}
}
}
--------------------编程问答-------------------- 只要是数字(我没加检测),随便你输,都帮你读出来!
控制台应用程序。(c#)
--------------------编程问答-------------------- 抱歉,上面的测试用的多余代码没删,这个是删了的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace 中文读数字
{
class Program
{
static string back ;
static bool lastass =false ;
static bool namedo = true;
static void Main(string[] args)
{
string num = "";
string a, b, c, d;
num = Console.ReadLine();
Regex fast = new Regex(@"^0");
while (fast.IsMatch(num))
{
num = num.Remove(0, 1);
}
switch (num.Length % 4)
{
case 0:
break;
case 3:
num = "0" + num;
break;
case 2:
num = "00" + num;
break;
case 1:
num = "000" + num;
break;
}
int x = num.Length / 4;
char[] ss = num.ToCharArray();
num = "";
for (int i = 0; i < x; i++)
{
if (i == (x-1))
lastass = true;
a = ss[ss.Length - (4 * i + 4)].ToString();
b = ss[ss.Length - (4 * i + 3)].ToString();
c = ss[ss.Length - (4 * i + 2)].ToString();
d = ss[ss.Length - (4 * i + 1)].ToString();
num = change(a, b, c, d) + name(i + 1) + num;
Console.WriteLine(num);
}
Console .WriteLine (num );
Console .ReadLine ();
}
public static string name(int i)
{
back = "";
if (!namedo )
return "";
if (i == 1)
return "";
else if (i > 1)
{
if (i % 2 != 0)
{
for (int c = (i - 1) / 2; c > 0; c--)
back += "亿";
}
else if (i % 2 == 0)
{
back += "万";
for (int c = (i - 1 - 1) / 2; c > 0; c--)
back += "亿";
}
return back;
}
return "error";
}
public static string change(string a,string b,string c,string d)
{
back="";
switch (a)
{
case "0":
back+="零";
break ;
default :
back+=a+"千";
break ;
}
switch (b)
{
case "0":
back+="零";
break ;
default :
back+=b+"百";
break ;
}
switch (c)
{
case "0":
back+="零";
break ;
default :
back+=c+"十";
break ;
}
switch (d)
{
case "0":
back+="";
break ;
default :
back+=d;
break ;
}
Regex last=new Regex (@"零$");
while (last .IsMatch (back ))
{
back =back.Remove (back.Length -1);
}
Regex fast = new Regex(@"^零");
while (fast.IsMatch(back)&&lastass )
{
back = back.Remove(0, 1);
}
Regex init3=new Regex ("零零零");
Regex init2=new Regex ("零零");
if (init3.IsMatch (back ))
{
back =back .Replace ("零零零","零");
}
if (init2.IsMatch (back ))
{
back =back .Replace ("零零","零");
}
if (back == "" && lastass)
namedo = false;
return back ;
}
static string changone(string s)
{
string a="";
switch (s)
{
case "1":
a = "一";
break;
case "2":
a = "二";
break;
case "3":
a = "三";
break;
case "4":
a = "四";
break;
case "5":
a = "五";
break;
case "6":
a = "六";
break;
case "7":
a = "七";
break;
case "8":
a = "八";
break;
case "9":
a = "九";
break;
}
return a;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace 中文读数字
{
class Program
{
static string back ;
static bool lastass =false ;
static bool namedo = true;
static void Main(string[] args)
{
string num = "";
string a, b, c, d;
num = Console.ReadLine();
Regex fast = new Regex(@"^0");
while (fast.IsMatch(num))
{
num = num.Remove(0, 1);
}
switch (num.Length % 4)
{
case 0:
break;
case 3:
num = "0" + num;
break;
case 2:
num = "00" + num;
break;
case 1:
num = "000" + num;
break;
}
int x = num.Length / 4;
char[] ss = num.ToCharArray();
num = "";
for (int i = 0; i < x; i++)
{
if (i == (x-1))
lastass = true;
a = ss[ss.Length - (4 * i + 4)].ToString();
b = ss[ss.Length - (4 * i + 3)].ToString();
c = ss[ss.Length - (4 * i + 2)].ToString();
d = ss[ss.Length - (4 * i + 1)].ToString();
num = change(a, b, c, d) + name(i + 1) + num;
}
Console .WriteLine (num );
Console .ReadLine ();
}
public static string name(int i)
{
back = "";
if (!namedo )
return "";
if (i == 1)
return "";
else if (i > 1)
{
if (i % 2 != 0)
{
for (int c = (i - 1) / 2; c > 0; c--)
back += "亿";
}
else if (i % 2 == 0)
{
back += "万";
for (int c = (i - 1 - 1) / 2; c > 0; c--)
back += "亿";
}
return back;
}
return "error";
}
public static string change(string a,string b,string c,string d)
{
back="";
switch (a)
{
case "0":
back+="零";
break ;
default :
back+=changone(a)+"千";
break ;
}
switch (b)
{
case "0":
back+="零";
break ;
default :
back += changone(b) + "百";
break ;
}
switch (c)
{
case "0":
back+="零";
break ;
default :
back += changone(c) + "十";
break ;
}
switch (d)
{
case "0":
back+="";
break ;
default :
back += changone(d);
break ;
}
Regex last=new Regex (@"零$");
while (last .IsMatch (back ))
{
back =back.Remove (back.Length -1);
}
Regex fast = new Regex(@"^零");
while (fast.IsMatch(back)&&lastass )
{
back = back.Remove(0, 1);
}
Regex init3=new Regex ("零零零");
Regex init2=new Regex ("零零");
if (init3.IsMatch (back ))
{
back =back .Replace ("零零零","零");
}
if (init2.IsMatch (back ))
{
back =back .Replace ("零零","零");
}
if (back == "" && lastass)
namedo = false;
return back ;
}
static string changone(string s)
{
string a="";
switch (s)
{
case "1":
a = "一";
break;
case "2":
a = "二";
break;
case "3":
a = "三";
break;
case "4":
a = "四";
break;
case "5":
a = "五";
break;
case "6":
a = "六";
break;
case "7":
a = "七";
break;
case "8":
a = "八";
break;
case "9":
a = "九";
break;
}
return a;
}
}
}
效果:
--------------------编程问答-------------------- 怎么又是这个???
public string GetMoney(double dd)--------------------编程问答-------------------- 是读出来,不是叫你们写出来 --------------------编程问答-------------------- 呵呵 我不知道写怎么办? --------------------编程问答-------------------- 学习学习 --------------------编程问答-------------------- 很强大啊 --------------------编程问答-------------------- 读音?那要有录音啊,根据字,调用指定文件,把声音放出来
{
string s = dd.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}");
string value = Regex.Replace(d, ".", delegate(Match m)
{ return "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟萬億兆京垓秭穰"[m.Value[0] - '-'].ToString(); });
return value;
}
补充:.NET技术 , C#