/* 创造者:菜刀打好博客

 * 创建日期: 2014年09一个月04号码

 * 特征:Money类型转换

 *

 */

namespace Net.String.ConsoleApplication

{

    using System;

    using System.Collections.Generic;

public class MoneyHelper

    {

        public static string[] chineseDigits = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };

/// <summary>

        /// 把金额转换为汉字表示的数量,小数点后四舍五入保留两位

        /// </summary>

        /// <param name="amount">小写金额</param>

        /// <returns>人民币大写</returns>

        public static string amountToChinese(decimal amount)

        {

            if (amount > 99999999999999.99m || amount < -99999999999999.99m)

            {

                throw new Exception("參数值超出同意范围 (-99999999999999.99 ~ 99999999999999.99)!

");

            }

            // 假设是负数,先转换为正数

            bool negative = false;

            if (amount < 0)

            {

                negative = true;

                amount = amount * (-1);

            }

            // 乘以100再进行四舍五入。实现小数保留2位

            decimal temp_r = Round(amount, 2);

            int temp = Convert.ToInt32(temp_r * 100);

            int numFen = (int)(temp % 10);    // 分

            temp = temp / 10;

            int numJiao = (int)(temp % 10);   // 角

            temp = temp / 10;

            // temp 眼下是金额的整数部分

            //

            int[] parts = new int[20]; // 当中的元素是把原来金额整数部分切割为值在 0~9999 之间的数的各个部分

            int numParts = 0;          // 记录把原来金额整数部分切割为了几个部分(每部分都在 0~9999 之间)

            for (int i = 0; ; i++)

            {

                if (temp == 0)

                {

                    break;

                }

                int part = (int)(temp % 10000);

                parts[i] = part;

                numParts++;

                temp = temp / 10000;

            }

            //

            bool beforeWanIsZero = true; // 标志“万”以下一级是不是 0

            string chineseStr = "";

            for (int i = 0; i < numParts; i++)

            {

                string partChinese = partTranslate(parts[i]);

if (i % 2 == 0)

                {

                    if ("".Equals(partChinese))

                    {

                        beforeWanIsZero = true;

                    }

                    else

                    {

                        beforeWanIsZero = false;

                    }

                }

if (i != 0)

                {

                    if (i % 2 == 0)

                    {

                        chineseStr = "亿" + chineseStr;

                    }

                    else

                    {

                        // 假设“万”相应的part为0。而“万”以下一级不为0,则不加“万”。而加“零”

                        if ("".Equals(partChinese) && !beforeWanIsZero)

                        {

                            chineseStr = "零" + chineseStr;

                        }

                        else

                        {

                            // 假设"万"的部分不为0,而"万"前面的部分小于1000大于0。则万后面应该跟“零”

                            if (parts[i - 1] < 1000 && parts[i - 1] > 0)

                            {

                                chineseStr = "零" + chineseStr;

                            }

                            chineseStr = "万" + chineseStr;

                        }

                    }

                }

                chineseStr = partChinese + chineseStr;

            }

// 最后处理

            if ("".Equals(chineseStr))  // 整数部分为 0, 则表达为"零元"

            {

                chineseStr = chineseDigits[0];

            }

            else if (negative)          // 整数部分不为 0, 而且原金额为负数

            {

                chineseStr = "负" + chineseStr;

            }

            chineseStr = chineseStr + "元";

            if ((numFen == 0) && (numJiao == 0))

            {

                chineseStr = chineseStr + "整";

            }

            else if (numFen == 0)  // 0 分。角数不为 0

            {

                chineseStr = chineseStr + chineseDigits[numJiao] + "角";

            }

            else                   // “分”数不为 0

            {

                if (numJiao == 0)

                {

                    chineseStr = chineseStr + "零" + chineseDigits[numFen] + "分";

                }

                else

                {

                    chineseStr = chineseStr + chineseDigits[numJiao] + "角" + chineseDigits[numFen] + "分";

                }

            }

            return chineseStr;

        }

/// <summary>

        /// 把一个 0~9999 之间的整数转换为汉字的字符串,假设是 0 则返回 ""

        /// </summary>

        /// <param name="amountPart"></param>

        /// <returns></returns>

        public static string partTranslate(int amountPart)

        {

            if (amountPart < 0 || amountPart >= 10000)

            {

                throw new Exception("參数必须是大于等于 0,小于 10000 的整数!");

            }

string[] units = new string[] { "", "拾", "佰", "仟" };

int temp = amountPart;

string amountStr = amountPart.ToString();

            int amountStrLength = amountStr.Length;

bool lastIsZero = true; // 在从低位往高位循环时,记录上一位数字是不是 0

            string chineseStr = "";

for (int i = 0; i < amountStrLength; i++)

            {

                if (temp == 0) // 高位已无数据

                {

                    break;

                }

                int digit = temp % 10;

                if (digit == 0) // 取到的数字为 0

                {

                    if (!lastIsZero) // 前一个数字不是 0,则在当前汉字串前加“零”字;

                    {

                        chineseStr = "零" + chineseStr;

                    }

                    lastIsZero = true;

                }

                else   // 取到的数字不是 0

                {

                    chineseStr = chineseDigits[digit] + units[i] + chineseStr;

                    lastIsZero = false;

                }

                temp = temp / 10;

            }

            return chineseStr;

        }

public static decimal Round(decimal data, int digits)

        {

            double i = Math.Pow(10, digits);

            decimal temp = (data * (decimal)(i));

            int intData = (int)temp;

            decimal digData = temp - intData;

if (digData >= decimal.Parse("0.5"))

                intData++;

            string format = digits > 0 ? "0." : "0";

            for (int n = 0; n < digits; n++)

            {

                format += "0";

            }

            return Convert.ToDecimal(((decimal)(intData / i)).ToString(format));

}

       

        /// <summary>

        /// 把金额转换为汉字表示

        /// </summary>

        public static string RMBEncode(decimal num)

        {

            try

            {

                #region

                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=佰拾元角分

#endregion

                //循环取出每一位须要转换的值

                for (i = 0; i < j; i++)

                {

                    #region

                    str3 = str4.Substring(i, 1);          //取出需转换的某一位的值

                    temp = Convert.ToInt32(str3);      //转换为数字

                    if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15))

                    {

                        #region

                        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;

                            }

                        }

                        #endregion

                    }

                    else

                    {

                        #region

                        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

                            {

                                #region

                                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;

                                    }

                                }

                                #endregion

                            }

                        }

                        #endregion

                    }

                    if (i == (j - 11) || i == (j - 3))

                    {

                        ch2 = str2.Substring(i, 1);

                    }

                    str5 = str5 + ch1 + ch2;

if (i == j - 1 && str3 == "0")

                    {

                        str5 = str5 + '整';

                    }

                    #endregion

                }

                if (num == 0)

                {

                    str5 = "零元整";

                }

                return str5;

            }

            catch

            {

                return "非法数据";

            }

        }

       

    }

}

版权声明:本文博主原创文章,博客,未经同意不得转载。

C#量转换为汉字表达的更多相关文章

  1. 如何利用java把文件中的Unicode字符转换为汉字

    有些文件中存在Unicode字符和非Unicode字符,如何利用java快速的把文件中的Unicode字符转换为汉字而不影响文件中的其他字符呢, 我们知道虽然java 在控制台会把Unicode字符直 ...

  2. javascript 使用数组+循环+条件实现数字转换为汉字的简单方法。

    这几天,博主碰到了几道关于数字转汉字的javascript算法题,在网上找了很多的答案,发现都有点复杂,于是我决定自己写一篇关于这种算法题的简单解法,以下是博主自己的见解,有不足的地方请多指教. 接下 ...

  3. UTF8字符串转换为汉字 c#

    using System; /// <summary> /// UTF8字符串转换为汉字用的类 /// 转换如"\\u8d35"之类的字符串为对应的汉字 /// < ...

  4. 【转】UTF8字符串转换为汉字 c#,转自游戏开发主席

    using System; /// <summary> /// UTF8字符串转换为汉字用的类 /// 转换如"\\u8d35"之类的字符串为对应的汉字 /// < ...

  5. java实现 阿拉伯数字转换为汉字数字 算法

    package test; public class NumberFormatTest { static String[] units = { "", "十", ...

  6. jmeter响应数据Unicode编码转换为汉字

    2018-07-09     10:24:34 每次用jmeter做接口测试时,响应信息中文总是显示Unicode编码格式,每次都要在网上寻找这一段转换的代码,但是我发现在网上找这段代码有点麻烦,像我 ...

  7. C# 实现十六进制Unicode编码字符串转换为汉字

    网上找了几个方法,但是运行之后会报错,提示要解析的字符串格式不正确.然后我猜想可能是传入的字符串 \u60a8\u4eca\u65e5\u5df2\u7b7e\u5230 中带"\" ...

  8. java实现 阿拉伯数字转换为汉字数字(转载)

    public class VedioExtractSpeech { public static void main(String[] args) { System.out.println(" ...

  9. .Net(c#)汉字和Unicode编码互相转换

    {"Tilte": "\u535a\u5ba2\u56ed", "Href": "http://www.cnblogs.com&q ...

随机推荐

  1. Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view...

    Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tabl ...

  2. 你好,C++(34)有一只叫做多利的羊 6.2.4 拷贝构造函数

    6.2.4  拷贝构造函数 在C++世界中,除了需要使用构造函数直接创建一个新的对象之外,有时还需要根据已经存在的某个对象创建它的一个副本,就像那只叫做多利的羊一样,我们希望根据一只羊创建出来另外一只 ...

  3. 面向对象设计模式之Interpreter解释器模式(行为型)

    动机:在软件构建过程中 ,如果某一特定领域的问题比较复杂,类似的模式不断重复出现,如果使用普通的编程方式来实现将面临非常频繁的变化.在这种情况下,将特定领域的问题表达为某种语法规则的句子,然后构建一个 ...

  4. [javascript]String添加trim和reverse方法

    function trim() { var start, end; start = 0; end = this.length - 1; while(start <= end && ...

  5. MLlib-聚类

    聚类 例子 流聚类 例子 聚类 MLlib支持k-means聚类,一种最常用的聚类方法,将数据点聚成指定数据的簇.MLlib实现了一种k-means++的并行变种,叫做kmeansII.MLlib的实 ...

  6. Primary Expression

    Primary expressions are the building blocks of more complex expressions. They are literals, names, a ...

  7. file upload download

    1. 文件上传与下载 1.1 文件上传 案例: 注册表单/保存商品等相关模块! --à 注册选择头像 / 商品图片 (数据库:存储图片路径 / 图片保存到服务器中指定的目录) 文件上传,要点: 前台: ...

  8. springmvc基于xml配置文件

    web.xml 配置文件 <!-- springmvc 配置入口 --> <servlet> <servlet-name>mvc-dispatcher</se ...

  9. strcmp() Anyone?

    uva11732:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...

  10. Autodesk 开源 3D 打印机

    Autodesk 开源 3D 打印机 Autodesk在知识共享-署名-相同方式共享许可证下公开了其3D打印机Ember的树脂.机械设计.电路图纸的细节,在GNU GPL许可证下公开了固件.打印机运行 ...