C#实现阿拉伯数字(小写金额)到大写中文(大写金额)的转换
/// <summary>
/// 本类实现阿拉伯数字到大写中文的转换
/// 该类没有对非法数字进行判别,请事先自己判断数字是否合法
/// </summary>
public class ChineseNum
{
//小写转大写
public static string GetChineseNum(string p_num)
{
ChineseNum cn = new ChineseNum();
return cn.NumToChn(p_num);
}
//小写金额转大写金额
public static string GetUpperMoney(double p_Money)
{
ChineseNum cn = new ChineseNum();
return cn.GetMoneyChinese(p_Money);
}
//转换数字
private char CharToNum(char x)
{
string stringChnNames = "零一二三四五六七八九";
string stringNumNames = "0123456789";
return stringChnNames[stringNumNames.IndexOf(x)];
}
//转换万以下整数
private string WanStrToInt(string x)
{
string[] stringArrayLevelNames = new string[4] { "", "十", "百", "千" };
string ret = "";
int i;
for (i = x.Length - 1; i >= 0; i--)
if (x[i] == '0')
{
ret = CharToNum(x[i]) + ret;
}
else
{
ret = CharToNum(x[i]) + stringArrayLevelNames[x.Length - 1 - i] + ret;
}
while ((i = ret.IndexOf("零零")) != -1)
{
ret = ret.Remove(i, 1);
}
if (ret[ret.Length - 1] == '零' && ret.Length > 1)
{
ret = ret.Remove(ret.Length - 1, 1);
}
if (ret.Length >= 2 && ret.Substring(0, 2) == "一十")
{
ret = ret.Remove(0, 1);
}
return ret;
}
//转换整数
private string StrToInt(string x)
{
int len = x.Length;
string ret, temp;
if (len <= 4)
{
ret = WanStrToInt(x);
}
else if (len <= 8)
{
ret = WanStrToInt(x.Substring(0, len - 4)) + "万";
temp = WanStrToInt(x.Substring(len - 4, 4));
if (temp.IndexOf("千") == -1 && temp != "")
ret += "零" + temp;
else
ret += temp;
}
else
{
ret = WanStrToInt(x.Substring(0, len - 8)) + "亿";
temp = WanStrToInt(x.Substring(len - 8, 4));
if (temp.IndexOf("千") == -1 && temp != "")
{
ret += "零" + temp;
}
else
{
ret += temp;
}
ret += "万";
temp = WanStrToInt(x.Substring(len - 4, 4));
if (temp.IndexOf("千") == -1 && temp != "")
{
ret += "零" + temp;
}
else
{
ret += temp;
}
}
int i;
if ((i = ret.IndexOf("零万")) != -1)
{
ret = ret.Remove(i + 1, 1);
}
while ((i = ret.IndexOf("零零")) != -1)
{
ret = ret.Remove(i, 1);
}
if (ret[ret.Length - 1] == '零' && ret.Length > 1)
{
ret = ret.Remove(ret.Length - 1, 1);
}
return ret;
}
//转换小数
private string StrToDouble(string x)
{
string ret = "";
for (int i = 0; i < x.Length; i++)
{
ret += CharToNum(x[i]);
}
return ret;
}
private string NumToChn(string x)
{
if (x.Length == 0)
{
return "";
}
string ret = "";
if (x[0] == '-')
{
ret = "负";
x = x.Remove(0, 1);
}
if (x[0].ToString() == ".")
{
x = "0" + x;
}
if (x[x.Length - 1].ToString() == ".")
{
x = x.Remove(x.Length - 1, 1);
}
if (x.IndexOf(".") > -1)
{
ret += StrToInt(x.Substring(0, x.IndexOf("."))) + "点" + StrToDouble(x.Substring(x.IndexOf(".") + 1));
}
else
{
ret += StrToInt(x);
}
return ret;
}
//金额转换
private string GetMoneyChinese(Double Money)
{
int i;
string mstrSource;
if (Money == 0)
{
return "";
}
mstrSource = Money.ToString("#0.00");
i = mstrSource.IndexOf(".");
if (i > 0) { mstrSource = mstrSource.Replace(".", ""); }
if (mstrSource.Substring(0, 1) == "0") { mstrSource = mstrSource.Remove(0, 1); }
mstrSource = NumstrToChinese(mstrSource);
if (mstrSource.Length == 0) { return ""; }
//负
if (Money < 0)
{
mstrSource = "负" + mstrSource;
}
mstrSource = mstrSource.Replace("0", "零");
mstrSource = mstrSource.Replace("1", "壹");
mstrSource = mstrSource.Replace("2", "贰");
mstrSource = mstrSource.Replace("3", "叁");
mstrSource = mstrSource.Replace("4", "肆");
mstrSource = mstrSource.Replace("5", "伍");
mstrSource = mstrSource.Replace("6", "陆");
mstrSource = mstrSource.Replace("7", "柒");
mstrSource = mstrSource.Replace("8", "捌");
mstrSource = mstrSource.Replace("9", "玖");
mstrSource = mstrSource.Replace("M", "亿");
mstrSource = mstrSource.Replace("W", "万");
mstrSource = mstrSource.Replace("S", "仟");
mstrSource = mstrSource.Replace("H", "佰");
mstrSource = mstrSource.Replace("T", "拾");
mstrSource = mstrSource.Replace("Y", "圆");
mstrSource = mstrSource.Replace("J", "角");
mstrSource = mstrSource.Replace("F", "分");
if (mstrSource.Substring(mstrSource.Length - 1, 1) != "分")
{
mstrSource = mstrSource + "整";
}
return mstrSource;
}
//金额转换
private string NumstrToChinese(string numstr)
{
int i;
int j;
string mstrChar;
string[] mstrFlag = new string[4];
string mstrReturn = "";
bool mblnAddzero = false;
mstrFlag[0] = "";
mstrFlag[1] = "T";
mstrFlag[2] = "H";
mstrFlag[3] = "S";
for (i = 1; i <= numstr.Length; i++)
{
j = numstr.Length - i;
mstrChar = numstr.Substring(i - 1, 1);
if (mstrChar != "0" && j > 1) { mstrReturn = mstrReturn + mstrChar + mstrFlag[(j - 2) % 4]; }
if (mstrChar == "0" && mblnAddzero == false)
{
mstrReturn = mstrReturn + "0";
mblnAddzero = true;
}
if (j == 14)
{
if (mstrReturn.Substring(mstrReturn.Length - 1) == "0")
{ mstrReturn = mstrReturn.Substring(0, mstrReturn.Length - 1) + "W0"; }
else
{ mstrReturn = mstrReturn + "W"; }
}
if (j == 2)
{
if (mstrReturn.Substring(mstrReturn.Length - 1, 1) == "0")
{ mstrReturn = mstrReturn.Substring(0, mstrReturn.Length - 1) + "Y0"; }
else
{ mstrReturn = mstrReturn + "Y"; }
//元
}
if (j == 6)
{
if (mstrReturn.Length > 2)
{
if (mstrReturn.Substring(mstrReturn.Length - 2) != "M0")
{
if (mstrReturn.Substring(mstrReturn.Length - 1) == "0")
{ mstrReturn = mstrReturn.Substring(0, mstrReturn.Length - 1) + "W0"; }
else
{ mstrReturn = mstrReturn + "W"; }
}
}
else
{
if (mstrReturn.Substring(mstrReturn.Length - 1) == "0")
{ mstrReturn = mstrReturn.Substring(0, mstrReturn.Length - 1) + "W0"; }
else
{ mstrReturn = mstrReturn + "W"; }
}
}
if (j == 10)
{
if (mstrReturn.Substring(mstrReturn.Length - 1) == "0")
{ mstrReturn = mstrReturn.Substring(0, mstrReturn.Length - 1) + "M0"; }
else
{ mstrReturn = mstrReturn + "M"; }
}
if (j == 0 && mstrChar != "0") { mstrReturn = mstrReturn + mstrChar + "F"; }
if (j == 1 && mstrChar != "0") { mstrReturn = mstrReturn + mstrChar + "J"; }
if (mstrChar != "0") { mblnAddzero = false; }
}
if (mstrReturn.Substring(0, 1) == "1" && mstrReturn.Substring(1, 1) == mstrFlag[1]) { mstrReturn = mstrReturn.Substring(1); }
if (mstrReturn.Substring(mstrReturn.Length - 1, 1) == "0") { mstrReturn = mstrReturn.Substring(0, mstrReturn.Length - 1); }
if (mstrReturn.Substring(0, 1) == "0") { mstrReturn = mstrReturn.Substring(1); }
if (mstrReturn.Substring(mstrReturn.Length - 1, 1) == "M" || mstrReturn.Substring(mstrReturn.Length - 1, 1) == "W" || mstrReturn.Substring(mstrReturn.Length - 1, 1) == "S" || mstrReturn.Substring(mstrReturn.Length - 1, 1) == "H" || mstrReturn.Substring(mstrReturn.Length - 1, 1) == "T") { mstrReturn = mstrReturn + "Y"; }
return mstrReturn;
}
}
C#实现阿拉伯数字(小写金额)到大写中文(大写金额)的转换的更多相关文章
- C#:小写金额转换为大写
#region 小写金额转换为大写 public static string CurrToChnNum(double Currnum) { string sResult = ""; ...
- SQL标量值函数:小写金额转大写
我们日常开发业务系统中,作为统计报表中,特别是财务报表,显示中文金额经常遇到. 转换大小写的方法有很多,以下是从数据库函数方面解决这一问题. 效果如图: 调用:SELECT dbo.[Fn_Conve ...
- jsp页面输入小写金额转大写
<script> function chineseNumber(num){ if (isNaN(num) || num > Math.pow(10, 12)) return &quo ...
- php小写金额转大写
public static function amountInWords($num) { if (!is_numeric($num) || empty($num)) ...
- js 小写金额转大写
function smalltoBIG(n) { var fraction = ['角', '分']; var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', ...
- js 人民币小写金额转换为大写
function smalltoBIG(n) { var fraction = ['角', '分']; var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', ...
- JS将人民币小写金额转换为大写
/** 数字金额大写转换(可以处理整数,小数,负数) */ function smalltoBIG(n) { var fraction = ['角', '分']; var digit = ['零', ...
- java将小写金额转换为大写的工具类
public class Tool { private static final String UNIT = "万千佰拾亿千佰拾万千佰拾元角分"; ...
- 【转】Delphi货币类型转中文大写金额
unit TU2.Helper.Currency; interface ): string; ): string; implementation uses System.SysUtils, Syste ...
随机推荐
- Ubuntu 定时任务中的环境变量设置
背景 1,定时任务命令 crontab -e 2,默认的环境变量 SHELL=/bin/sh PATH=/usr/bin:/bin PWD=/home/owl LANG=zh_CN.UTF- SHLV ...
- C#仪器数据文件解析-XPS文件
XPS为微软推出的类似于Adobe PDF的一种文件格式,个人认为XPS很好,但毕竟PDF已经被大家所熟知,因此XPS的使用很少,也少有仪器数据输出为该格式. XPS百度百科:https://baik ...
- jvm系列(十):如何优化Java GC「译」
本文由CrowHawk翻译,是Java GC调优的经典佳作. 本文翻译自Sangmin Lee发表在Cubrid上的"Become a Java GC Expert"系列文章的第三 ...
- Sql Server 数据库中调用dll文件
1.首先新建一个空的解决方案,并添加一个类库,代码如下,编译并生产dll using System; using System.Collections.Generic; using System.Da ...
- c# 【MVC】WebApi开发实例
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using S ...
- Jquery跨域读取城市天气预报信息
最新项目中遇到一个问题,页面需要显示一些天气信息,但是部署网站的服务器没连接外网,只有客户端的电脑能连外网,于是想用js去实现这个功能. 刚开始找了一些方法,单独在浏览器中可以使用,但是在项目中运行的 ...
- NOIP初赛 之 哈夫曼树
哈夫曼树 种根据我已刷的初赛题中基本每套的倒数第五或第六个不定项选择题就有一个关于哈夫曼树及其各种应用的题,占:0-1.5分:然而我针对这个类型的题也多次不会做,so,今晚好好研究下哈夫曼树: 概念: ...
- 前端页面卡顿、也许是DOM操作惹的祸?
界面上UI的更改都是通过DOM操作实现的,并不是通过传统的刷新页面实现 的.尽管DOM提供了丰富接口供外部调用,但DOM操作的代价很高,页面前端代码的性能瓶颈也大多集中在DOM操作上,所以前端性能优化 ...
- MSDN-9月杂志推荐
序言:MSDN Magazine频道每月都会推出杂志,提供给广大开发者免费阅读,文章的作者往往都是国内外技术大牛.而且翻译良好(机器翻译除外)排班工整,是技术进阶的绝美助力! 本系列文章会关注MSDN ...
- ubuntu 下修改文件访问权限chmod 777 -R *血的教训!没事别乱开权限!用谁开谁的就行。。。最后不要用这个命令,文件操作全部改用终端
本文转自: 个人建议 Ubuntu下修改目录权限命令如下:chmod 600 name (只有所有者有读和写的权限)chmod 644 name (所有者有读和写的权限,组用户只有读的权限)chmod ...