2013-06-27 6 views
0
private string BDTtakatowords(Int64 BDTtaka) 
{ 
    string result=""; 
    if ((BDTtaka >= 1) && (BDTtaka <= 10)) 
    { 
     if ((BDTtaka % 10) == 1) result = "One"; 
     if ((BDTtaka % 10) == 2) result = "Two"; 
     if ((BDTtaka % 10) == 3) result = "Three"; 
     if ((BDTtaka % 10) == 4) result = "Four"; 
     if ((BDTtaka % 10) == 5) result = "Five"; 
     if ((BDTtaka % 10) == 6) result = "Six"; 
     if ((BDTtaka % 10) == 7) result = "Seven"; 
     if ((BDTtaka % 10) == 8) result = "Eight"; 
     if ((BDTtaka % 10) == 9) result = "Nine"; 
     if ((BDTtaka % 10) == 0) result = "Ten"; 
    } 
    if (BDTtaka > 9 && BDTtaka < 20) 
    { 
     if (BDTtaka == 11) result = "Eleven"; 
     if (BDTtaka == 12) result = "Twelve"; 
     if (BDTtaka == 13) result = "Thirteen"; 
     if (BDTtaka == 14) result = "Forteen"; 
     if (BDTtaka == 15) result = "Fifteen"; 
     if (BDTtaka == 16) result = "Sixteen"; 
     if (BDTtaka == 17) result = "Seventeen"; 
     if (BDTtaka == 18) result = "Eighteen"; 
     if (BDTtaka == 19) result = "Nineteen"; 
     if (BDTtaka == 20) result = "Twenty"; 
    } 
    if (BDTtaka > 20 && (BDTtaka/10) == 2 && (BDTtaka % 10) == 0) result = "Twenty"; 
    if (BDTtaka > 20 && (BDTtaka/10) == 3 && (BDTtaka % 10) == 0) result = "Thirty"; 
    if (BDTtaka > 20 && (BDTtaka/10) == 4 && (BDTtaka % 10) == 0) result = "Forty"; 
    if (BDTtaka > 20 && (BDTtaka/10) == 5 && (BDTtaka % 10) == 0) result = "Fifty"; 
    if (BDTtaka > 20 && (BDTtaka/10) == 6 && (BDTtaka % 10) == 0) result = "Sixty"; 
    if (BDTtaka > 20 && (BDTtaka/10) == 7 && (BDTtaka % 10) == 0) result = "Seventy"; 
    if (BDTtaka > 20 && (BDTtaka/10) == 8 && (BDTtaka % 10) == 0) result = "Eighty"; 
    if (BDTtaka > 20 && (BDTtaka/10) == 9 && (BDTtaka % 10) == 0) result = "Ninty"; 

    if (BDTtaka > 20 && (BDTtaka/10) == 2 && (BDTtaka % 10) != 0) 
    { 
     if ((BDTtaka % 10) == 0) result = "Twenty"; 
     if ((BDTtaka % 10) == 1) result = "Twenty One"; 
     if ((BDTtaka % 10) == 2) result = "Twenty Two"; 
     if ((BDTtaka % 10) == 3) result = "Twenty Three"; 
     if ((BDTtaka % 10) == 4) result = "Twenty Four"; 
     if ((BDTtaka % 10) == 5) result = "Twenty Five"; 
     if ((BDTtaka % 10) == 6) result = "Twenty Six"; 
     if ((BDTtaka % 10) == 7) result = "Twenty Seven"; 
     if ((BDTtaka % 10) == 8) result = "Twenty Eight"; 
     if ((BDTtaka % 10) == 9) result = "Twenty Nine"; 
    } 
    if (BDTtaka > 20 && (BDTtaka/10) == 3 && (BDTtaka % 10) != 0) 
    { 
     if ((BDTtaka % 10) == 1) result = "Thirty One"; 
     if ((BDTtaka % 10) == 2) result = "Thirty Two"; 
     if ((BDTtaka % 10) == 3) result = "Thirty Three"; 
     if ((BDTtaka % 10) == 4) result = "Thirty Four"; 
     if ((BDTtaka % 10) == 5) result = "Thirty Five"; 
     if ((BDTtaka % 10) == 6) result = "Thirty Six"; 
     if ((BDTtaka % 10) == 7) result = "Thirty Seven"; 
     if ((BDTtaka % 10) == 8) result = "Thirty Eight"; 
     if ((BDTtaka % 10) == 9) result = "Thirty Nine"; 
    } 
    if (BDTtaka > 20 && (BDTtaka/10) == 4 && (BDTtaka % 10) != 0) 
    { 
     if ((BDTtaka % 10) == 1) result = "Forty One"; 
     if ((BDTtaka % 10) == 2) result = "Forty Two"; 
     if ((BDTtaka % 10) == 3) result = "Forty Three"; 
     if ((BDTtaka % 10) == 4) result = "Forty Four"; 
     if ((BDTtaka % 10) == 5) result = "Forty Five"; 
     if ((BDTtaka % 10) == 6) result = "Forty Six"; 
     if ((BDTtaka % 10) == 7) result = "Forty Seven"; 
     if ((BDTtaka % 10) == 8) result = "Forty Eight"; 
     if ((BDTtaka % 10) == 9) result = "Forty Nine"; 
    } 
    if (BDTtaka > 20 && (BDTtaka/10) == 5 && (BDTtaka % 10) != 0) 
    { 
     if ((BDTtaka % 10) == 1) result = "Fifty One"; 
     if ((BDTtaka % 10) == 2) result = "Fifty Two"; 
     if ((BDTtaka % 10) == 3) result = "Fifty Three"; 
     if ((BDTtaka % 10) == 4) result = "Fifty Four"; 
     if ((BDTtaka % 10) == 5) result = "Fifty Five"; 
     if ((BDTtaka % 10) == 6) result = "Fifty Six"; 
     if ((BDTtaka % 10) == 7) result = "Fifty Seven"; 
     if ((BDTtaka % 10) == 8) result = "Fifty Eight"; 
     if ((BDTtaka % 10) == 9) result = "Fifty Nine"; 
    } 
    if (BDTtaka > 20 && (BDTtaka/10) == 6 && (BDTtaka % 10) != 0) 
    { 
     if ((BDTtaka % 10) == 1) result = "Sixty One"; 
     if ((BDTtaka % 10) == 2) result = "Sixty Two"; 
     if ((BDTtaka % 10) == 3) result = "Sixty Three"; 
     if ((BDTtaka % 10) == 4) result = "Sixty Four"; 
     if ((BDTtaka % 10) == 5) result = "Sixty Five"; 
     if ((BDTtaka % 10) == 6) result = "Sixty Six"; 
     if ((BDTtaka % 10) == 7) result = "Sixty Seven"; 
     if ((BDTtaka % 10) == 8) result = "Sixty Eight"; 
     if ((BDTtaka % 10) == 9) result = "Sixty Nine"; 
    } 
    if (BDTtaka > 20 && (BDTtaka/10) == 7 && (BDTtaka % 10) != 0) 
    { 
     if ((BDTtaka % 10) == 1) result = "Seventy One"; 
     if ((BDTtaka % 10) == 2) result = "Seventy Two"; 
     if ((BDTtaka % 10) == 3) result = "Seventy Three"; 
     if ((BDTtaka % 10) == 4) result = "Seventy Four"; 
     if ((BDTtaka % 10) == 5) result = "Seventy Five"; 
     if ((BDTtaka % 10) == 6) result = "Seventy Six"; 
     if ((BDTtaka % 10) == 7) result = "Seventy Seven"; 
     if ((BDTtaka % 10) == 8) result = "Seventy Eight"; 
     if ((BDTtaka % 10) == 9) result = "Seventy Nine"; 
    } 
    if (BDTtaka > 20 && (BDTtaka/10) == 8 && (BDTtaka % 10) != 0) 
    { 
     if ((BDTtaka % 10) == 1) result = "Eighty One"; 
     if ((BDTtaka % 10) == 2) result = "Eighty Two"; 
     if ((BDTtaka % 10) == 3) result = "Eighty Three"; 
     if ((BDTtaka % 10) == 4) result = "Eighty Four"; 
     if ((BDTtaka % 10) == 5) result = "Eighty Five"; 
     if ((BDTtaka % 10) == 6) result = "Eighty Six"; 
     if ((BDTtaka % 10) == 7) result = "Eighty Seven"; 
     if ((BDTtaka % 10) == 8) result = "Eighty Eight"; 
     if ((BDTtaka % 10) == 9) result = "Eighty Nine"; 
    } 
    if (BDTtaka > 20 && (BDTtaka/10) == 9 && (BDTtaka % 10) != 0) 
    { 
     if ((BDTtaka % 10) == 1) result = "Ninty One"; 
     if ((BDTtaka % 10) == 2) result = "Ninty Two"; 
     if ((BDTtaka % 10) == 3) result = "Ninty Three"; 
     if ((BDTtaka % 10) == 4) result = "Ninty Four"; 
     if ((BDTtaka % 10) == 5) result = "Ninty Five"; 
     if ((BDTtaka % 10) == 6) result = "Ninty Six"; 
     if ((BDTtaka % 10) == 7) result = "Ninty Seven"; 
     if ((BDTtaka % 10) == 8) result = "Ninty Eight"; 
     if ((BDTtaka % 10) == 9) result = "Ninty Nine"; 
    } 
    return result; 
} 

protected void Button1_Click(object sender, System.EventArgs e) 
{ 
    TextBox2.Text = BDTtaka(Convert.ToInt64(TextBox1.Text)); 
} 

Проблема: когда я вхожу в Text1, например, 20 или 20000 или 2000000, тогда показываю только Таку или только Тысяча Така или Лак Така только соответственно. Но должен показывать двадцать или двадцать тысяч двадцати лаков соответственно.Перевод валюты Бангладеш на слова

+0

Я не могу ответить на ваш вопрос, потому что я не очень понимаю, это возможно потому, что мне не хватает знаний ABOU т валюты в Бангладеш, но вам было бы лучше, если бы вы заменили вашу свернутую (а также несколько багги) коллекцию операторов if с помощью простого массива числовых слов с 99 элементами. –

+0

В нашу бангладешскую валюту: 1 = одна така, 10 = десять така, 100 = сто така, 1000 = одна тысяча така, 99000 = девяносто девять тысяч така, 100000 = одна лак така, 1000000 = десять лак така, 10000000 = one crore taka –

+0

Мне было поручено поддерживать такой код раньше. Ради вашего здравомыслия и здравомыслия ваших преемников, пожалуйста, найдите время, чтобы упростить этот код. Как только вы это сделаете, я думаю, вы обнаружите, что ваш вопрос намного легче ответить, поскольку это будет естественное расширение шаблона, который вы использовали для упрощения первоначального подхода. –

ответ

0

Это может (?) Ответить на ваш вопрос. Кажется, вы пытаетесь сделать то же самое, только более запутанно. converting numbers in to words C#

0

Я не уверен, если это именно то, как вы говорите, но я дам ему попробовать:

//this array specifies the names of the powers of 10 
static Tuple<int, string>[] powers = 
{ 
    new Tuple<int, string>(0, ""), 
    new Tuple<int, string>(3, "thousand"), 
    new Tuple<int, string>(5, "lac"), 
    new Tuple<int, string>(7, "crore") 
}; 

//this array specifies the digits' names 
static string[] digits = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; 
static string[] extendedDigits = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fivteen", "sixteen", "seventeen", "eighteen", "nineteen" }; 
static string[] tensWords = { "", "", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" }; 

private static string NumberToWords(long number) 
{ 
    var sb = new StringBuilder(); 

    //begin with the left most digit (greatest power of 10) 
    for (int i = powers.Length - 1; i >= 0; --i) 
    { 
     //translate the current part only (for a known power of 10) 
     //usually part is a 3-digit number 
     int part = (int)(number/(long)Math.Pow(10, powers[i].Item1)); 
     //if the part is 0, we don't have to add anything 
     if (part > 0) 
     { 
      //extract the hundreds 
      int hundreds = part/100; 
      if(hundreds > 9) 
       throw new ArgumentException(number + " is too large and cannot be expressed."); 
      if (hundreds > 0) 
      { 
       //if there are hundreds, copy them to the output 
       sb.Append(digits[hundreds]); 
       sb.Append(" hundred "); 
      } 
      //convert the next two digits 
      sb.Append(TwoDigitNumberToWord(part % 100)); 
      sb.Append(" "); 
      //and append the name of the power of 10 
      sb.Append(powers[i].Item2); 
      sb.Append(" "); 
      //subtract the currently managed part 
      number -= part * (long)Math.Pow(10, powers[i].Item1); 
     } 
    } 
    return sb.ToString(); 
} 

private static string TwoDigitNumberToWord(int number) 
{ 
    //one digit case 
    if (number < 10) 
     return digits[number]; 
    //special case 10 <= n <= 19 
    if (number < 20) 
     return extendedDigits[number - 10]; 
    int tens = number/10; 
    int ones = number % 10; 
    //concatenate the word from the two digits' words 
    return tensWords[tens] + " " + digits[ones]; 
} 
0

Метание это в дикой природе (и дакинга):

static string hundreds(Int64 n) 
{ 
    n %= 100; 
    switch (n) 
    { 
    case 0: 
     return "zero"; 
    case 10: 
     return "ten"; 
    case 11: 
     return "eleven"; 
    case 12: 
     return "twelve"; 
    case 13: 
     return "thirteen"; 
    case 14: 
     return "fourteen"; 
    case 15: 
     return "fifteen"; 
    case 16: 
     return "sixteen"; 
    case 17: 
     return "seventeen"; 
    case 18: 
     return "eighteen"; 
    case 19: 
     return "nineteen"; 
    } 

    string s = ""; 
    switch (n/10) 
    { 
    case 2: 
     s = "twenty"; 
     break; 
    case 3: 
     s = "thirty"; 
     break; 
    case 4: 
     s = "fourty"; 
     break; 
    case 5: 
     s = "fifty"; 
     break; 
    case 6: 
     s = "sixty"; 
     break; 
    case 7: 
     s = "seventy"; 
     break; 
    case 8: 
     s = "eighty"; 
     break; 
    case 9: 
     s = "ninety"; 
     break; 
    } 

    if (n/10 > 0 && n%10 > 0) 
     s += " "; 

    switch (n % 10) 
    { 
    case 1: 
     s += "one"; 
     break; 
    case 2: 
     s += "two"; 
     break; 
    case 3: 
     s += "three"; 
     break; 
    case 4: 
     s += "four"; 
     break; 
    case 5: 
     s += "five"; 
     break; 
    case 6: 
     s += "six"; 
     break; 
    case 7: 
     s += "seven"; 
     break; 
    case 8: 
     s += "eight"; 
     break; 
    case 9: 
     s += "nine"; 
     break; 
    } 

    return s; 
} 

static string thousands(Int64 n) 
{ 
    string s = ""; 
    int h = (int)(n%1000)/100; 
    if (h > 0) 
    { 
     s += hundreds(h) + " hundred"; 
     int r = (int)n % 100; 
     if (r == 0) 
      return s; 

     if (r > 0 && r < 10 && !string.IsNullOrEmpty(s)) 
      s += " and "; 
     else if (!string.IsNullOrEmpty(s) && r > 0) 
      s += " "; 
    } 
    return s + hundreds(n); 
} 

static string milionths(Int64 n) 
{ 
    string s = ""; 
    int t = (int)(n%10000000)/1000; 
    if (t > 0 && t < 100) 
    { 
     s += hundreds(t) + " thousand"; 
    } 
    else 
    { 
     t /= 100; 
     if (t > 0) 
      s += hundreds(t) + " lac"; 
    } 
    int r = (int)n % 1000; 
    if (r == 0) 
     return s; 

    if (r > 0 && r < 10 && !string.IsNullOrEmpty(s)) 
     s += " and "; 
    else if (!string.IsNullOrEmpty(s) && r > 0) 
     s += " "; 

    return s + thousands(n); 
} 

static string crore(Int64 n) 
{ 
    string s = ""; 
    Int64 m = n/10000000; 
    if (m > 0) 
    { 
     s += thousands(m) + " crore"; 
     int r = (int)n % 10000000; 
     if (r == 0) 
      return s; 

     if (r > 0 && r < 10 && !string.IsNullOrEmpty(s)) 
      return s + " and one"; 
     else if (!string.IsNullOrEmpty(s) && r > 0) 
      s += " "; 
    } 
    return s + milionths(n); 
} 

static void Main(string[] args) 
{ 
    System.Console.WriteLine(crore(9999)); 
    System.Console.WriteLine(crore(10000)); 
    System.Console.WriteLine(crore(10100)); 
    System.Console.WriteLine(crore(10101)); 
    System.Console.WriteLine(crore(99999)); 
    System.Console.WriteLine(crore(100000)); 
    System.Console.WriteLine(crore(100001)); 
    System.Console.WriteLine(crore(1000000)); 
    System.Console.WriteLine(crore(10000000)); 
    System.Console.WriteLine(crore(10000001)); 

    System.Console.ReadLine(); 
} 

Он печатает:

nine thousand nine hundred ninety nine 
ten thousand 
ten thousand one hundred 
ten thousand one hundred and one 
ninety nine thousand nine hundred ninety nine 
one lac 
one lac and one 
ten lac 
one crore 
one crore and one