如何在我的文本框中使用此代码 [英] How Can Use This Code In My text Box

查看:89
本文介绍了如何在我的文本框中使用此代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的文本框中使用此代码

How Can Use This Code In My text Box

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

推荐答案

我们不做你的功课:它设置为原因。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但是我们不打算为你做这一切!



说实话,只是在互联网上找到随机代码,似乎做你想做的事情并尝试使用它而不了解它是如何工作的并不会让你走得太远:它可能最终会让你完成你的作业,但它不会帮助你通过课程......
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...

这篇关于如何在我的文本框中使用此代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆