简体   繁体   English

C#Money数值到书面价值

[英]C# Money numeric value to written value

I am receiving data which I am basically recreating the image of a check for filing purposes. 我收到的数据基本上是为了生成支票而重新创建支票的图像。 I have the monetary amount of the check coming in, but I need to create the written out value. 我有支票的货币金额,但是我需要创建已写出的值。

ie $2400.22 >> Two Thousand Four Hundred Dollars and Twenty Two Cents ie $ 2400.22 >>二十四百美元和二十二分

Was wondering if any one out there knows of a pre-packaged class or something so I don't have to rewrite the wheel. 想知道是否有人知道预包装的课程或什么,所以我不必重写轮子。

Cheers. 干杯。

您可以查看此代码高尔夫球问题中的BenAlabasters 答案 ,以了解如何执行此操作。

I kind of like this one. 我有点喜欢这个。 it's limited to doing check dollar amounts as it is but could be tweaked to work in a more general way. 它仅限于按原样进行支票美元金额,但可以进行调整以更通用的方式工作。

    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" };

编写起来并不难,例如,通过快速搜索即可找到 CodeProject文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM