문제

기본적으로 제출 목적으로 수표의 이미지를 재현하는 데이터를 받고 있습니다. 수표의 금전적 금액이 들어오고 있지만 기록 된 값을 만들어야합니다.

IE $ 2400.22 >> 2 천 4 백 달러와 22 센트

거기에있는 사람이 사전 포장 된 클래스 또는 무언가를 알고 있는지 궁금해서 휠을 다시 쓸 필요가 없습니다.

건배.

도움이 되었습니까?

해결책

Benalabasters를 볼 수 있습니다 대답 이것에서 코드 골프 이것을하는 방법에 대한 아이디어에 대한 질문.

다른 팁

글을 쓰기 어렵지 않지만 빠른 검색이 나타납니다. 이것 예를 들어 CodeProject 기사.

나는 이런 것 같다. 수표 달러 금액을 그대로 수행하는 것으로 제한되지만보다 일반적인 방식으로 일하기 위해 조정할 수 있습니다.

    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" };
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top