说明:金额转换可以转换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);
        }
    }

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

  1. delphi 金额大小写转换函数

    {*------------------------------------------------ 金额大小写转换函数 @author 王云盼 @version V1506.01 在delphi7测 ...

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

      1:计算double值四舍五入的方法 对小数数值进行四舍五入,首先应该确认保留小数位, 如果数值的小数精度大于保留小数位,那么开始四舍五入计算.四舍五入的方法非常简单,在所有要丢失精度的小数位中加 ...

  3. 金额大小写转换和input失去焦点触发事件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. java 金额的大小写转换类

    /** *金额大小写转换工具类 */ public class MoneyUtil { /** 大写数字 */ private static final String[] NUMBERS = { &q ...

  5. VSCode设置大小写转换的快捷键

    本文已同步到专业技术网站 www.sufaith.com, 该网站专注于前后端开发技术与经验分享, 包含Web开发.Nodejs.Python.Linux.IT资讯等板块. VSCode在默认情况下没 ...

  6. 【Linux基础】VI命令模式下大小写转换

    [开始位置] ---- 可以指定开始的位置,默认是光标的当前位置 gu ---- 把选择范围全部小写 gU ---- 把选择范围全部大写 [结束位置] ---- 可以跟着类似的w,6G,gg等定位到错 ...

  7. 用vim处理字符的大小写转换

    转载: http://blog.csdn.net/ruixj/article/details/3765385 http://www.linuxsong.org/2010/09/vim-convert- ...

  8. php 字母大小写转换的函数

    分享下,在php编程中,将字母大小写进行转换的常用函数. 1.将字符串转换成小写strtolower(): 该函数将传入的字符串参数所有的字符都转换成小写,并以小定形式放回这个字符串 2.将字符转成大 ...

  9. JAVA之旅(十六)——String类,String常用方法,获取,判断,转换,替换,切割,子串,大小写转换,去除空格,比较

    JAVA之旅(十六)--String类,String常用方法,获取,判断,转换,替换,切割,子串,大小写转换,去除空格,比较 过节耽误了几天,我们继续JAVA之旅 一.String概述 String时 ...

随机推荐

  1. [转]SpringMVC Controller&View数据传递

    Spring MVC3在controller和视图之间传递参数的方法:   一, 从controller往视图传递值, controller---->视图   1)简单类型,如int, Stri ...

  2. Linux下grep显示前后几行信息

    Linux下grep显示前后几行信息 标准unix/linux下的grep通过下面參数控制上下文 grep -C 5 foo file 显示file文件里匹配foo字串那行以及上下5行grep -B ...

  3. 发现新大陆-JMX

    今天接触到这个东西,觉得好有趣,可以用很多第三方的显示层jar包直接在UI界面上操作指定的java对象,网上将这个东西的也挺多的,我个人觉得这个比webServer还强大了.webserver只是公布 ...

  4. Win7以上 32/64位系统隐藏托盘图标

    前两天有朋友找我,让帮忙写个小工具,隐藏windows的托盘图标,想想最近在家也不想做太复杂的事,也好几年没写过windows上的小工具了,就答应了.想来挺简单的事,没想到还是有点小插曲的.特地来博客 ...

  5. Koa – 更加强大的下一代 Node.js Web 框架

    Koa 是 Express 的开发团队设计的下一代 Web 框架,其目的是为 Web 应用程序提供更小,更具表现力,更坚实的基础.Koa 没有核捆绑任何中间件,并提供了一​​套优雅的方法,使服务器端开 ...

  6. Swift3.0_注释,警告,todo的写法

    一. Swift3.0 不支持写Prama的注释了. 也不能打#warning了. 二. 取代方法 1. 注释: //MARK: - setter & getter 2.警告 //FIXME: ...

  7. 新找到一个安装Android SDk的方法-记录

    此方法需使用国内的镜像,但是国内镜像网速不一定要很快. 迅雷下载工具这个是必须的. 今天注意到SDK目录下有一个temp文件夹,打开看了看发现就是缓存的目录,因此想到直接从镜像站下载相应的包来替换,测 ...

  8. mobile 更改hosts

    在Android下,/etc是link到/system/etc的,我们需要修改/system/etc/hosts来实现.但是这个文件是只读,不能通过shell直接修改.可以通过连接到PC上使用adb来 ...

  9. SQL Server技术问题之游标优缺点

    分类: MS SQL SERVER 支持三种类型的游标:Transact_SQL 游标,API 服务器游标和客户游标. (1) Transact_SQL 游标 Transact_SQL 游标是由DEC ...

  10. dp - Google Code jam Qualification Round 2015 --- Problem B. Infinite House of Pancakes

    Problem B. Infinite House of Pancakes Problem's Link:   https://code.google.com/codejam/contest/6224 ...