.NET下金额大小写转换

说明:金额转换可以转换50位的数值,单位从分到级。对于中间部分是否显示零,
可以根据修改TranslateJInen()函数修改。
中间数值为零的去掉不显示
 public string GetChCapitalNumber(string canshu)

中间数值为零的显示
public string GetChCapitalNumberFill(string canshu)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace Jinen_Translate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

//判断粘贴而来的数据是否满足条件,只能为数字和小数点
        public static bool IsNumeric(string value)
        {
            return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
        }

//计算小数点之后一共有几位数字  2 结果为0  3.258 结果为3
        //超出十位的小数点记录为10位
        public int Fun_getLengthPoint(string inputText)
        {
            int PointLength = 0;
            int count = inputText.Length;
            //记录输入字符串的长度
            int jilu = 0;
            /*记录小数点所在的位置
            当出现小数点时,停止循环*/
            for (int i = 0; i < count; i++)
            {
                jilu++;
                if (inputText[i] == ‘.‘)
                    break;
            }

if (jilu == count)
            {
                PointLength = 0;
                return PointLength;
            }

//记录输入字符串的长度
            else
            {
                int dian_point = inputText.IndexOf(".");
                //采集小数点以后的字符串。   int dian = input.IndexOf(".");记录小数点所在的位置
                string base_last = inputText.Substring(dian_point + 1, count - jilu);
                //记录小数点之后数的位数
                PointLength = base_last.Length;
                //返回小数点一共有几位数字
                return PointLength;
            }
        }

//Dupliacte() 创建重复字符串
        public string Duplicate(string mstr, int ncnt)
        {
            // 使用 StringBuilder 加速字符串重复创建的速度
            StringBuilder dstr = new StringBuilder();
            int icnt = 0;

for (icnt = 0; icnt < ncnt; icnt++)
            {
                dstr.Append(mstr);
            }

return dstr.ToString();
        }

//截取字符串
        public string Left(string mdata, int leng)
        {
            mdata = mdata.Trim();
            if (mdata.Length < leng)
                leng = mdata.Length;

return mdata.Trim().Substring(0, leng);
        }

//取得所有的字符串
        public String Fun_getText(string inputText)
        {
            //记录输入字符串的长度
            int jilu = 0;
            int count = inputText.Length;
            /*记录小数点所在的位置
            当出现小数点时,停止循环*/
            for (int i = 0; i < count; i++)
            {
                jilu++;
                if (inputText[i] == ‘.‘)
                    break;
            }

if (jilu == count)
            {
                string number1 = inputText;
                return number1;
            }
            else
            {
                //采集小数点以前的字符串
                string base_hade = inputText.Substring(0, jilu - 1);
                int dian_point = inputText.IndexOf(".");
                //采集小数点以后的字符串。   int dian = input.IndexOf(".");记录小数点所在的位置
                string base_last = inputText.Substring(dian_point + 1, count - jilu);
                //形成新的字符串,去掉了小数点
                string number1 = base_hade + base_last;
                return number1;
            }
        }
    
        // #region GetFourChNumber() 取得每四位数的中文位数字 (万、亿、兆...)
        public string GetFourChNumber(int iDigit)
        {
            string sDigit = "";

// 限于整数态的上限,‘京’之后的中文数字是用不到
            switch (iDigit)
            {
                case 0:
                    sDigit = "";
                    break;
                case 1:
                    sDigit = "万";
                    break;
                case 2:
                    sDigit = "亿";
                    break;
                case 3:
                    sDigit = "兆";
                    break;
                case 4:
                    sDigit = "京";
                    break;
                case 5:
                    sDigit = "垓";
                    break;
                case 6:
                    sDigit = "秭";
                    break;
                case 7:
                    sDigit = "穰";
                    break;
                case 8:
                    sDigit = "沟";
                    break;
                case 9:
                    sDigit = "涧";
                    break;
                case 10:
                    sDigit = "正";
                    break;
                case 11:
                    sDigit = "载";
                    break;
                case 12:
                    sDigit = "极";
                    break;
                default:
                    sDigit = "∞";
                    break;
            }

return sDigit;
        }

//获得数据的一个一个变为大写数字
        public string ChCapitalNumber(string NStr)
        {
            NStr = Left(NStr, 1);

switch (NStr)
            {
                case "0":
                    NStr = "零";
                    break;

case "1":
                    NStr = "壹";
                    break;

case "2":
                    NStr = "贰";
                    break;

case "3":
                    NStr = "参";
                    break;

case "4":
                    NStr = "肆";
                    break;

case "5":
                    NStr = "伍";
                    break;

case "6":
                    NStr = "陆";
                    break;

case "7":
                    NStr = "柒";
                    break;

case "8":
                    NStr = "捌";
                    break;

case "9":
                    NStr = "玖";
                    break;

default:
                    NStr = "?";
                    break;
            }
            return NStr;
        }

//省略零的结果转换
        public string GetChCapitalNumber(string NumStr)
        {
            string ChStr = "", ChSubStr = "", tmpStr = "";
            int iCnt = 0, jCnt = 0, kCnt = 0, lCnt = -1, nCnt = 0;
            nCnt = NumStr.Length;

// 中文数字以四位数为一个处理单位(万、亿、兆、京....)
            iCnt = nCnt % 4;
            NumStr = Duplicate("0", 4 - iCnt) + NumStr;
            nCnt = NumStr.Length;

for (iCnt = 0; iCnt < nCnt; iCnt += 4)
            {
                lCnt++;
                ChSubStr = "";

for (jCnt = 0; jCnt < 4; jCnt++)
                {
                    kCnt = nCnt - iCnt - jCnt - 1;
                    tmpStr = ChCapitalNumber(NumStr.Substring(kCnt, 1));

if (tmpStr == "零")
                    {
                        if (Left(ChSubStr, 1) != "零" && Left(ChSubStr, 1) != "")
                            ChSubStr = tmpStr + ChSubStr;
                    }
                    else
                    {
                        switch (jCnt)
                        {
                            case 0:
                                ChSubStr = tmpStr;
                                break;
                            case 1:
                                ChSubStr = tmpStr + "拾" + ChSubStr;
                                break;
                            case 2:
                                ChSubStr = tmpStr + "佰" + ChSubStr;
                                break;
                            case 3:
                                ChSubStr = tmpStr + "仟" + ChSubStr;
                                break;
                            default:
                                ChSubStr = tmpStr + ChSubStr;
                                break;
                        }
                    }
                }

if (ChSubStr == "零" && Left(ChStr, 1) != "" && Left(ChStr, 1) != "零")
                    ChStr = "零" + ChStr;
                else
                {
                    if (ChSubStr != "")
                    {
                        // 取得10000的次方中文数字
                        // 限于整数态的上限,‘京’之后的中文数字是用不到
                        ChStr = ChSubStr + GetFourChNumber(lCnt) + ChStr;
                    }
                }
            }

if (ChStr == "")
                ChStr = "零";
            else if (Left(ChStr, 1) == "零")
                ChStr = ChStr.Substring(1, ChStr.Length - 1);

return ChStr;
        }

//包括零的结果运算
        public string GetChCapitalNumberFill(string NumStr)
        {
            string ChStr = "", ChSubStr = "", tmpStr = "";
            int iCnt = 0, jCnt = 0, kCnt = 0, lCnt = -1, nCnt = 0;
            nCnt = NumStr.Length;

// 中文数字以四位数为一个处理单位(万、亿、兆、京....)
            iCnt = nCnt % 4;
            NumStr = Duplicate("0", 4 - iCnt) + NumStr;
            nCnt = NumStr.Length;

for (iCnt = 0; iCnt < nCnt; iCnt += 4)
            {
                lCnt++;
                ChSubStr = "";

for (jCnt = 0; jCnt < 4; jCnt++)
                {
                    kCnt = nCnt - iCnt - jCnt - 1;
                    tmpStr = ChCapitalNumber(NumStr.Substring(kCnt, 1));

switch (jCnt)
                    {
                        case 0:
                            ChSubStr = tmpStr;
                            break;
                        case 1:
                            ChSubStr = tmpStr + "拾" + ChSubStr;
                            break;
                        case 2:
                            ChSubStr = tmpStr + "佰" + ChSubStr;
                            break;
                        case 3:
                            ChSubStr = tmpStr + "仟" + ChSubStr;
                            break;
                        default:
                            ChSubStr = tmpStr + ChSubStr;
                            break;
                    }
                }

if (ChSubStr != "")
                {
                    // 取得10000的次方中文数字
                    // 限于整数态的上限,‘京’之后的中文数字是用不到
                    ChStr = ChSubStr + GetFourChNumber(lCnt) + ChStr;
                }
            }

while (Left(ChStr, 1) == "零" && ChStr.Length > 1)
            {
                if (Left(ChStr, 1) == "零")
                    ChStr = ChStr.Substring(2, ChStr.Length - 2);
            }

return ChStr;
        }
       // 金额转换
        public String TranslateJinen(string canshu)
        {
            string result;
            if (!IsNumeric(canshu))
            {
                result = "Error!";
                return result;
            }
            else
            {
                if (canshu.Length > 50)
                {
                    result = "数值太大,转换失败!";
                    return result;
                }
                else
                {
                    int t = Fun_getLengthPoint(canshu);  //t为传人参数的小数点后面的位数 如2.56 t=2
                    if (t == 0)
                    {
                        string jin = Fun_getText(canshu);
                        jin = GetChCapitalNumberFill(canshu);
                        string Tran_jin = jin + "元零角零分";
                        return Tran_jin;
                    }
                    else
                    {
                        string jin = Fun_getText(canshu) + "00";                 //以防只输入一位小数,人为地在后面加两个0
                        string jin_head = jin.Substring(0, jin.Length - 2 - t);      //截取小数点以后的两位数值                   
                        string jin_last_f = jin.Substring(jin_head.Length, 1);      //截取小数点之后的第一位数值  如45.235 结果为2
                        string jin_last_l = jin.Substring(jin_head.Length + 1, 1); //截取小数点之后的第二位数值  如45.235 结果为3
                        jin_head = GetChCapitalNumberFill(jin_head);
                        jin_last_f = ChCapitalNumber(jin_last_f);
                        jin_last_l = ChCapitalNumber(jin_last_l);
                        string Tran_jin = jin_head + "元" + jin_last_f + "角" + jin_last_l + "分";
                        return Tran_jin;
                    }
                }
            }

}

private void Action_Click(object sender, EventArgs e)
        {
            DaXieJinen.Text = TranslateJinen(Jinen.Text);
        }
    }

时间: 2024-10-05 04:24:20

.NET下金额大小写转换的相关文章

金额大小写转换

/// <summary> /// 金额大小写转换 /// </summary> /// <param name="number">金额</param> /// <returns></returns> private string ConvertToChinese(decimal number) { var s = number.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E

double四舍五入,商品金额大小写转换,设置货币的小数位数跟格式输出,进制转化

1:计算double值四舍五入的方法 对小数数值进行四舍五入,首先应该确认保留小数位, 如果数值的小数精度大于保留小数位,那么开始四舍五入计算.四舍五入的方法非常简单,在所有要丢失精度的小数位中加5,如果大于10则向前进位,最后计算出四舍五入的结果. /// <summary>计算double值四舍五入的方法 /// /// </summary> /// <param name="dbl">进行四舍五入的数值</param> /// &l

财务金额大小写转换

//==========================财务流程,货币小写转换为大写========[开始]=============================================== //对应数据库[LR_Form_SchemeInfo]的F_Id //二位数组定义:["表单名称/编号","小写金额标识","大写金额标识"] //表单名称:对应[表单设计]中的名称,而非流程 /* var needTransForm = new

数字金额大小写转换

有很多时候打印凭据的时候需要实现金额大写,例如 有多种写法来实现 例如一下几种 CREATE OR REPLACE FUNCTION chinese_number_program(p_input FLOAT) RETURN VARCHAR2 AS TYPE typ_money IS TABLE OF VARCHAR2(20); c_numbers typ_money := typ_money('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'); c

Birt金额大小写转换

1.reportMoneyConvert.js 1 function formatMoney(mnum) 2 { 3 var mnum = parseFloat(mnum); 4 var strOutput="",strTemp="",strInTemp=''; 5 var unitArray = new Array("圆万亿","仟佰拾","零壹贰叁肆伍陆柒捌玖"); 6 var mnumArray =

数字转换成中文大小写、金额大小写

将数字转换成中文大小写.金额大小写  /// <summary> /// 将数字转换成中文大写 /// </summary> /// <param name="Value">数字</param> /// <param name="bToUpper">是否转换成汉字大写 true表示大写 false表示小写</param> /// <param name="bMoney"

5.6.3.5 字符串的大小写转换

ECMAScript中涉及到字符串的大小写转换的方法有4 个:toLowerCase().toLocaleLowerCase().toUpperCase()和toLocaleUpperCase().其中,toLowerCase()和toUpperCase()是两个经典的方法,借鉴自java.lang.String中的同名方法.而 toLocaleLowerCase()和toLocaleUpperCase()方法则是针对特定地区的实现.对有些地区来说,针对地区的方法与其通用的方法得到的结果相同,但

C++大小写转换和性能(C语言,C++,API,STL一共4种方法)

大小写转换和性能 前言 本文主要讨论最基本的一些大小写转换函数和API,不讨论一些常见的字符串程序库里面的大小写转换接口,另外本文的落脚点是这些转换函数的性能和日常开发中遇到的一些问题. 不考虑范围 其实ctype.h里面有定义一套宏,就是不考虑字符是否落在A-Z,a-z范围,直接计算(直接用加减法或者使用位与或计算,差别不是很大).显然这样的效率是最高的,但是使用可能是有问题的,遇到中文或者其他友邦的一些字符,可能就转换错了,当然如果已经提前确认过输入会落在A-Z,a-z范围,则是可以使用这种

C++大小写转换和性能

大小写转换和性能 前言 本文主要讨论最基本的一些大小写转换函数和API,不讨论一些常见的字符串程序库里面的大小写转换接口,另外本文的落脚点是这些转换函数的性能和日常开发中遇到的一些问题. 不考虑范围 其实ctype.h里面有定义一套宏,就是不考虑字符是否落在A-Z,a-z范围,直接计算(直接用加减法或者使用位与或计算,差别不是很大).显然这样的效率是最高的,但是使用可能是有问题的,遇到中文或者其他友邦的一些字符,可能就转换错了,当然如果已经提前确认过输入会落在A-Z,a-z范围,则是可以使用这种