C#货币数值到书面值

C#货币数值到书面值,c#,currency,C#,Currency,我收到的数据基本上是为了存档而重新创建支票的图像。我有支票的货币金额,但我需要创建已写出的值 i、 e$2400.22>>贰仟肆佰美元贰角贰分 我想知道是否有人知道一个预先打包的类或其他东西,所以我不必重写轮子 干杯 这并不难写,但快速搜索会发现CodeProject文章,例如。你可以在这个问题中看看Benalabaster,了解如何做到这一点。我有点喜欢这个。它只限于做支票金额,但可以调整,以更普遍的方式工作 public static string ToWrittenValueStr

我收到的数据基本上是为了存档而重新创建支票的图像。我有支票的货币金额,但我需要创建已写出的值

i、 e$2400.22>>贰仟肆佰美元贰角贰分

我想知道是否有人知道一个预先打包的类或其他东西,所以我不必重写轮子


干杯

这并不难写,但快速搜索会发现CodeProject文章,例如。

你可以在这个问题中看看Benalabaster,了解如何做到这一点。

我有点喜欢这个。它只限于做支票金额,但可以调整,以更普遍的方式工作

    public static string ToWrittenValueString(this decimal number)
    {
        // convert the number to a usable value
        var numStr = Math.Round(number,2,MidpointRounding.AwayFromZero).ToString();

        // seperate the value before and after the decimal point
        var numParts = numStr.Split('.');

        IList<string> txt = new List<string>();

        // get the total number of digits in the number before the decimal point.
        var digits = numParts[0].Length;

        for (int n = 1; n <= digits; n++)
        {
            //this handles the hundreds, hundred thousands and hundred millions place
            if (n % 3 == 0)
            {
                txt.Add("HUNDRED");
                txt.Add(onesAndTeens[int.Parse(numParts[0][digits - n].ToString())]);

            }
            // this handles the two digits preceding the hundreds, hundred thousands and hundred millions place    
            if (n % 3 == 2 | (n == digits & n % 3!=0)) 
            {
                // this get's the integer equivilent of only those two digits
                var tmpnum = int.Parse(string.Join("", numParts[0].Skip(digits-n).Take(n % 3 == 2? 2: 1)));


                // this get's the name of the three didget grouping that the current digit's of interest are in i.e. thousand, million etc...  
                txt.Add(digitGroupName[((n - n % 3) / 3)]);
                // if the integer equivilent is less than 20 we use the onesAndTeens lookup table
                // if the integer equivilent is twenty or more we use the tens lookup for the most significant digit and the onesAndTeens lookup for the least significant digit
                if (tmpnum < 20)
                    txt.Add(onesAndTeens[tmpnum]); 
                else
                    txt.Add(string.Format("{0}{1}", tens[(tmpnum - tmpnum % 10) / 10], onesAndTeens[tmpnum % 10] ));
            }

        }

        return  string.Format("{0} AND {1}/100 DOLLARS", string.Join(" ", txt.Reverse()), numParts[1]);;
    }

    private static string[] onesAndTeens = new string[20] { "", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT","NINE","TEN","ELEVEN","TWELVE", "THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN","SEVENTEEN","EIGHTEEN","NINETEEN"};
    private static string[] tens = new string[10] {"","","TWENTY","THIRTY","FOURTY","FIFTY","SIXTY","SEVENTY","EIGHTY","NINETY"};
    private static string[] digitGroupName = new string[4] {"","THOUSAND","MILLION","BILLION" };
公共静态字符串到rittenvaluestring(此十进制数)
{
//将数字转换为可用值
var numStr=Math.Round(数字,2,中点舍入.AwayFromZero).ToString();
//将小数点前后的值分开
var numParts=numStr.Split('.');
IList txt=新列表();
//获取小数点前数字的总位数。
var digits=numParts[0]。长度;

对于(int n=1;n)而言,远远优于CodeProject 1。