2014-01-27 48 views
1

我有這樣的代碼來獲取數字單詞和它的作品,但我有一個要求,我的應用程序,這就是:數量的話​​貨幣小數

例1:什麼節目呢,讓我們這個數字: 27,59 - 代碼將帶我到「巴哈,但是:

例2:當數字是27.00時,結果是:doua zeci si sapte si 00 bani,and the the結果我希望只有當某個數字有.00才能切割'si 00 bani'並且從27.00 =獲得比doua zeci si sapte lei比doua zeci si sapte lei si 00 bani。 謝謝

private static string[] _ones = 
     { 
      "", 
      "unu", 
      "doua", 
      "trei", 
      "patru", 
      "cinci", 
      "sase", 
      "sapte", 
      "opt", 
      "noua" 
     }; 

     private static string[] _teens = 
     { 
      "zece", 
      "unsprezece", 
      "doisprezece", 
      "treisprezece", 
      "paisprezece", 
      "cincisprezece", 
      "saisprezece", 
      "saptisprezece", 
      "optsprezece", 
      "nouasprezece" 
     }; 

     private static string[] _tens = 
     { 
      "", 
      "zece", 
      "douazeci", 
      "treizeci", 
      "patruzeci", 
      "cincizeci", 
      "saizeci", 
      "saptezeci", 
      "optzeci", 
      "nouazeci" 
     }; 

     // US Nnumbering: 
     private static string[] _thousands = 
     { 
      "", 
      "mie", 
      "milion", 
      "miliard", 
      "trilion", 
      "catralion" 
     }; 
string digits, temp; 
      bool showThousands = false; 
      bool allZeros = true; 

      // Use StringBuilder to build result 
      StringBuilder builder = new StringBuilder(); 
      // Convert integer portion of value to string 
      digits = ((long)value).ToString(); 
      // Traverse characters in reverse order 
      for (int i = digits.Length - 1; i >= 0; i--) 
      { 
       int ndigit = (int)(digits[i] - '0'); 
       int column = (digits.Length - (i + 1)); 

       // Determine if ones, tens, or hundreds column 
       switch (column % 3) 
       { 
        case 0:  // Ones position 
         showThousands = true; 
         if (i == 0) 
         { 
          // First digit in number (last in loop) 
          temp = String.Format("{0} ", _ones[ndigit]); 
         } 
         else if (digits[i - 1] == '1') 
         { 
          // This digit is part of "teen" value 
          temp = String.Format("{0} ", _teens[ndigit]); 
          // Skip tens position 
          i--; 
         } 
         else if (ndigit != 0) 
         { 
          // Any non-zero digit 
          temp = String.Format("{0} ", _ones[ndigit]); 
         } 
         else 
         { 
          // This digit is zero. If digit in tens and hundreds 
          // column are also zero, don't show "thousands" 
          temp = String.Empty; 
          // Test for non-zero digit in this grouping 
          if (digits[i - 1] != '0' || (i > 1 && digits[i - 2] != '0')) 
           showThousands = true; 
          else 
           showThousands = false; 
         } 

         // Show "thousands" if non-zero in grouping 
         if (showThousands) 
         { 
          if (column > 0) 
          { 
           temp = String.Format("{0}{1}{2}", 
            temp, 
            _thousands[column/3], 
            allZeros ? " " : ", "); 
          } 
          // Indicate non-zero digit encountered 
          allZeros = false; 
         } 
         builder.Insert(0, temp); 
         break; 

        case 1:  // Tens column 
         if (ndigit > 0) 
         { 
          temp = String.Format("{0}{1}", 
           _tens[ndigit], 
           (digits[i + 1] != '0') ? " si " : " "); 
          builder.Insert(0, temp); 
         } 
         break; 

        case 2:  // Hundreds column 
         if (ndigit > 0) 
         { 
          temp = String.Format("{0} sute ", _ones[ndigit]); 
          builder.Insert(0, temp); 
         } 
         break; 
       } 
      } 

      builder.AppendFormat("lei si {0:00} bani", (value - (long)value) * 100); 


      // Capitalize first letter 
      return String.Format("{0}{1}", 
       Char.ToUpper(builder[0]), 
       builder.ToString(1, builder.Length - 1)); 

回答

1

您需要的是檢查小數值的條件。一個簡單的方法就是.net提供的Decimal.Remainder()方法。

只要改變這個

builder.AppendFormat("lei si {0:00} bani", (value - (long)value) * 100); 

要包括你00的條件...

// You always need "lei" right? 
builder.AppendFormat("lei"); 

// This code simply divides the decimal value by 1; and only adds "si NN bani" if there's a remainder 
if (Decimal.Remainder(value, 1) > 0) { 
    builder.AppendFormat(" si {0:00} bani", (value - (long)value) * 100); 
} 
+0

我總是需要林雷但我需要,而不是27.00說 'doua zeci SI sapte林雷' 無00 bani如果小數點是27.00,那麼只有當它是零時,您的代碼纔會刪除00 bani,只有當值爲0時我只需要在值爲27.00的情況下刪除'si 00 bani',如果小數點值爲00之後的實際數字 – user3144640

+0

@ user3144640:好的,我的草率的錯誤。固定! –

+1

你知道什麼人嗎?你自己是一個該死的神。 – user3144640