<bdo id='GlZja'></bdo><ul id='GlZja'></ul>

  • <tfoot id='GlZja'></tfoot>
    <i id='GlZja'><tr id='GlZja'><dt id='GlZja'><q id='GlZja'><span id='GlZja'><b id='GlZja'><form id='GlZja'><ins id='GlZja'></ins><ul id='GlZja'></ul><sub id='GlZja'></sub></form><legend id='GlZja'></legend><bdo id='GlZja'><pre id='GlZja'><center id='GlZja'></center></pre></bdo></b><th id='GlZja'></th></span></q></dt></tr></i><div id='GlZja'><tfoot id='GlZja'></tfoot><dl id='GlZja'><fieldset id='GlZja'></fieldset></dl></div>

    <legend id='GlZja'><style id='GlZja'><dir id='GlZja'><q id='GlZja'></q></dir></style></legend>

    <small id='GlZja'></small><noframes id='GlZja'>

        将整数转换为书面数字

        时间:2023-07-25

        <small id='z6yv8'></small><noframes id='z6yv8'>

        1. <i id='z6yv8'><tr id='z6yv8'><dt id='z6yv8'><q id='z6yv8'><span id='z6yv8'><b id='z6yv8'><form id='z6yv8'><ins id='z6yv8'></ins><ul id='z6yv8'></ul><sub id='z6yv8'></sub></form><legend id='z6yv8'></legend><bdo id='z6yv8'><pre id='z6yv8'><center id='z6yv8'></center></pre></bdo></b><th id='z6yv8'></th></span></q></dt></tr></i><div id='z6yv8'><tfoot id='z6yv8'></tfoot><dl id='z6yv8'><fieldset id='z6yv8'></fieldset></dl></div>
            <tbody id='z6yv8'></tbody>
        2. <legend id='z6yv8'><style id='z6yv8'><dir id='z6yv8'><q id='z6yv8'></q></dir></style></legend>

                <bdo id='z6yv8'></bdo><ul id='z6yv8'></ul>

                  <tfoot id='z6yv8'></tfoot>
                • 本文介绍了将整数转换为书面数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有没有一种有效的方法将整数转换成书面数字,例如:

                  Is there an efficient method of converting an integer into the written numbers, for example:

                  string Written = IntegerToWritten(21);
                  

                  将返回二十一".

                  有没有不涉及大量查找表的方法?

                  Is there any way of doing this that doesn't involve a massive look-up table?

                  推荐答案

                  这应该很好用:

                  public static class HumanFriendlyInteger
                  {
                      static string[] ones = new string[] { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
                      static string[] teens = new string[] { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
                      static string[] tens = new string[] { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
                      static string[] thousandsGroups = { "", " Thousand", " Million", " Billion" };
                  
                      private static string FriendlyInteger(int n, string leftDigits, int thousands)
                      {
                          if (n == 0)
                          {
                              return leftDigits;
                          }
                  
                          string friendlyInt = leftDigits;
                  
                          if (friendlyInt.Length > 0)
                          {
                              friendlyInt += " ";
                          }
                  
                          if (n < 10)
                          {
                              friendlyInt += ones[n];
                          }
                          else if (n < 20)
                          {
                              friendlyInt += teens[n - 10];
                          }
                          else if (n < 100)
                          {
                              friendlyInt += FriendlyInteger(n % 10, tens[n / 10 - 2], 0);
                          }
                          else if (n < 1000)
                          {
                              friendlyInt += FriendlyInteger(n % 100, (ones[n / 100] + " Hundred"), 0);
                          }
                          else
                          {
                              friendlyInt += FriendlyInteger(n % 1000, FriendlyInteger(n / 1000, "", thousands+1), 0);
                              if (n % 1000 == 0)
                              {
                                  return friendlyInt;
                              }
                          }
                  
                          return friendlyInt + thousandsGroups[thousands];
                      }
                  
                      public static string IntegerToWritten(int n)
                      {
                          if (n == 0)
                          {
                              return "Zero";
                          }
                          else if (n < 0)
                          {
                              return "Negative " + IntegerToWritten(-n);
                          }
                  
                          return FriendlyInteger(n, "", 0);
                      }
                  }
                  

                  (经过编辑以修复百万、十亿等的错误)

                  (Edited to fix a bug w/ million, billion, etc.)

                  这篇关于将整数转换为书面数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Parse 诉 TryParse 下一篇:如何在 C# 中将 IPv4 地址转换为整数?

                  相关文章

                • <i id='KUqTC'><tr id='KUqTC'><dt id='KUqTC'><q id='KUqTC'><span id='KUqTC'><b id='KUqTC'><form id='KUqTC'><ins id='KUqTC'></ins><ul id='KUqTC'></ul><sub id='KUqTC'></sub></form><legend id='KUqTC'></legend><bdo id='KUqTC'><pre id='KUqTC'><center id='KUqTC'></center></pre></bdo></b><th id='KUqTC'></th></span></q></dt></tr></i><div id='KUqTC'><tfoot id='KUqTC'></tfoot><dl id='KUqTC'><fieldset id='KUqTC'></fieldset></dl></div>
                  <legend id='KUqTC'><style id='KUqTC'><dir id='KUqTC'><q id='KUqTC'></q></dir></style></legend>

                  <small id='KUqTC'></small><noframes id='KUqTC'>

                      <bdo id='KUqTC'></bdo><ul id='KUqTC'></ul>
                  1. <tfoot id='KUqTC'></tfoot>