算法_数字转换为汉字数值_自己写了好多种_自认这是最简单的算法_
2017-12-06 22:18
246 查看
数值转换汉字数值问题, 请尊重楼主版权,转载注明出处!(2个小时尝试4种方式后的最简结果...)
class MyClass
{
static string Rel(int number)
{
//结果
string resule = "";
//用作替换数字的字符数组
string[] rep = new string[]
{ "零", "一", "二", "三", "四",
"五", "六", "七", "八", "九", };
//用于添加单位的数组
string[] unit = new string[]
{ "", "十", "百", "千", "万","十"
,"百" ,"千" ,"亿","十","百" ,"千"};
//取数字位数
int l= number.ToString().Length;
//循环取最后以为数字处理字符转换
for (int i = 0; i <l; i++)
{
//取最后位数值
int temp = number % 10;
//取剩余位
number = number/10;
//判断当前最后位为0
if (temp == 0)
{
//判断万位添加单位 万
if (i == 4)
resule += unit[4];
//判断亿位添加单位 亿
if (i == 8)
resule += unit[8];
//判断当前最后位是否需要加 零
if(resule!=""&&resule[resule.Length-1]!= '零'&& resule[resule.Length - 1] != '万'&& resule[resule.Length - 1] != '亿')
resule += rep[temp];
}
else
{
//当前位不是0 添加单位 添加数值
resule += unit[i];
resule += rep[temp];
}
}
//定义中间变量 倒叙结果
string str = resule;
resule = "";
for (int i = 0; i < str.Length; i++)
{
resule += str[str.Length - 1 - i];
}
return resule;
}
static void Main(string[] args)
{
Console.WriteLine(Rel(1560850500));;
Console.ReadLine();
}
class MyClass
{
static string Rel(int number)
{
//结果
string resule = "";
//用作替换数字的字符数组
string[] rep = new string[]
{ "零", "一", "二", "三", "四",
"五", "六", "七", "八", "九", };
//用于添加单位的数组
string[] unit = new string[]
{ "", "十", "百", "千", "万","十"
,"百" ,"千" ,"亿","十","百" ,"千"};
//取数字位数
int l= number.ToString().Length;
//循环取最后以为数字处理字符转换
for (int i = 0; i <l; i++)
{
//取最后位数值
int temp = number % 10;
//取剩余位
number = number/10;
//判断当前最后位为0
if (temp == 0)
{
//判断万位添加单位 万
if (i == 4)
resule += unit[4];
//判断亿位添加单位 亿
if (i == 8)
resule += unit[8];
//判断当前最后位是否需要加 零
if(resule!=""&&resule[resule.Length-1]!= '零'&& resule[resule.Length - 1] != '万'&& resule[resule.Length - 1] != '亿')
resule += rep[temp];
}
else
{
//当前位不是0 添加单位 添加数值
resule += unit[i];
resule += rep[temp];
}
}
//定义中间变量 倒叙结果
string str = resule;
resule = "";
for (int i = 0; i < str.Length; i++)
{
resule += str[str.Length - 1 - i];
}
return resule;
}
static void Main(string[] args)
{
Console.WriteLine(Rel(1560850500));;
Console.ReadLine();
}
相关文章推荐
- #简单实现数字转换为汉字项目 (留给自己用的)
- 伙伴算法 (Buddy Algorithm)简单描诉和自己的简单实现
- 一天一个算法题-简单的-递归-计算a+aa+aaa+...+aaaa...a的数值
- STL 中几个简单的数值算法实现
- VC 数字转换为人民币金额大写的简单算法实现
- 一元线性回归入门——自己写简单的回归算法
- 【HDU4313】Matrix 多校 解题报告+AC代码+思路+算法正确性证明,此为Kruskal贪心恶心版本,非自虐倾向慎入!建议想找解题报告的童鞋看简单版本的,这个我写给自己【目标达成 0.2%】
- 前端和算法实现:给网站上加上自己的水印(简单+复杂)
- 自己做题的简单的算法
- 帮助曾经像自己一样的小白,快速了解hog和svm,从而学会运用这两个算法来做些简单的行人检测
- 自己动手,丰衣足食——一个简单却高效的图像旋转算法
- 汉字姓名生成unicode码的简单算法
- 机器学习入门第一天,自己对数值预测算法的理解
- 深度学习 16. 反向传递算法最简单的理解与提高,BP算法,Backpropagation, 自己的心得。
- linux,简单介绍加密解密算法,和如何给自己发放安全证书
- 简单算法入门
- 有关数论的算法&简单代码
- 《编程之美》简单算法小结
- 数据集+树的一种最简单高效的算法
- C++实现简单的内存块自己主动管理