Click here to Skip to main content
16,011,428 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How Can Use This Code In My text Box
C#
public static string Convert(decimal number)
   {
       if (number == 0)
           return "ZERO";

       if (number < 0)
           return "MINUS " + Convert(Math.Abs(number));

       string words = String.Empty;

       long intPortion = (long)number;
       decimal fraction = (number - intPortion);
       int decimalPrecision = GetDecimalPrecision(number);

       fraction = CalculateFraction(decimalPrecision, fraction);

       long decPortion = (long)fraction;

       words = IntToWords(intPortion);
       if (decPortion > 0)
       {
           words += " POINT ";
           words += IntToWords(decPortion);
       }

       return words.Trim();
   }

   public static string IntToWords(long number)
   {
       if (number == 0)
           return "ZERO";

       if (number < 0)
           return "MINUS " + IntToWords(Math.Abs(number));

       string words = "";

       if ((number / 1000000000000000) > 0)
       {
           words += IntToWords(number / 1000000000000000) + " QUADRILLION ";
           number %= 1000000000000000;
       }

       if ((number / 1000000000000) > 0)
       {
           words += IntToWords(number / 1000000000000) + " TRILLION ";
           number %= 1000000000000;
       }

       if ((number / 1000000000) > 0)
       {
           words += IntToWords(number / 1000000000) + " BILLION ";
           number %= 1000000000;
       }

       if ((number / 1000000) > 0)
       {
           words += IntToWords(number / 1000000) + " MILLION ";
           number %= 1000000;
       }

       if ((number / 1000) > 0)
       {
           words += IntToWords(number / 1000) + " THOUSAND ";
           number %= 1000;
       }

       if ((number / 100) > 0)
       {
           words += IntToWords(number / 100) + " HUNDRED ";
           number %= 100;
       }

       if (number > 0)
       {
           if (words != String.Empty)
               words += "AND ";

           var unitsMap = new[] { "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN" };
           var tensMap = new[] { "ZERO", "TEN", "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY" };

           if (number < 20)
               words += unitsMap[number];
           else
           {
               words += tensMap[number / 10];
               if ((number % 10) > 0)
                   words += "-" + unitsMap[number % 10];
           }
       }

       return words.Trim();
   }

   private static int GetDecimalPrecision(decimal number)
   {
       return (Decimal.GetBits(number)[3] >> 16) & 0x000000FF;
   }

   private static decimal CalculateFraction(int decimalPrecision, decimal fraction)
   {
       switch(decimalPrecision)
       {
           case 1:
               return fraction * 10;
           case 2:
               return fraction * 100;
           case 3:
               return fraction * 1000;
           case 4:
               return fraction * 10000;
           case 5:
               return fraction * 100000;
           case 6:
               return fraction * 1000000;
           case 7:
               return fraction * 10000000;
           case 8:
               return fraction * 100000000;
           case 9:
               return fraction * 1000000000;
           case 10:
               return fraction * 10000000000;
           case 11:
               return fraction * 100000000000;
           case 12:
               return fraction * 1000000000000;
           case 13:
               return fraction * 10000000000000;
           default:
               return fraction * 10000000000000;
       }
   }
Posted
Updated 21-Apr-15 4:16am
v3
Comments
jk0391 21-Apr-15 9:10am    
Double click the textbox (in design view) to generate an onclick event where you can put code that will execute whenever you interact with the textbox.
Sergey Alexandrovich Kryukov 21-Apr-15 10:08am    
This is a code dump, nut a question. But you need to rewrite this code from non-programming language to programming. This is the language of a computer user manually typing repeated fragment of text using copy/paste. All you cases do the same, but program does it all in one line. Do you know what a function is? other abstractions?
—SA

1 solution

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!

And to be honest, just finding random code on the internet which seems to do what you want and trying to use it without understanding how it works is not going to get you very far at all: it may end up with you getting your homework done, but it won't help you pass the course...
 
Share this answer
 
Comments
Member 11280947 22-Apr-15 2:56am    
Thank You I solved By My self
OriginalGriff 22-Apr-15 3:16am    
Good!
Well done :thumbsup:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900