【问题标题】:Number to Turkish text app土耳其语文本应用程序的号码
【发布时间】:2010-11-17 09:08:18
【问题描述】:

我正在尝试转换文本框中输入的数字。但是我找不到正确的原因,不起作用。您也请看看。提前谢谢!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NumberToText
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void NumberControl()
        {
            if ((txtNumber.Text.Length>7))
            {
                MessageBox.Show("Please enter a smaller number");
            }
        }
        public void ReadNumber()
        {


            try
            {
                int ones, tens, hundreds, thousands, tenthousands, hundredthousands, 

millions;
                int number = Convert.ToInt32(txtNumber.Text);

                int[] array=new int[7];



                for (int j = 0; j < txtNumber.Text.Length; )  
                {

                    array[j] = (number / (10 ^ (txtNumber.Text.Length - 

(txtNumber.Text.Length - j)))) % 10;
                    j += 1;
                }
                if (txtSayi.Text.Length != 7) 
                {
                    for (int i = 6; i >= txtNumber.Text.Length; )
                    {
                        dizi[i] = 0;
                        i-=1;
                    }
                }

                  ones = array[0];
                  tens = array[1];
                  hundreds = array[2];
                  thousands = array[3];
                  tenthousands = array[4];
                  hundredthousands = array[5];
                  millions = array[6];

                //Converting to numbers in TURKISH Text

                string[] a_ones = { "", "Bir", "İki", "Üç", "Dört", "Beş", "Altı", 

"Yedi", "Sekiz", "Dokuz" };
                string[] a_tens = { "", "On", "Yirmi", "Otuz", "Kırk", "Elli", 

"Altmış", "Yetmiş", "Seksen", "Doksan" };
                string[] a_hundreds = { "", "Yüz", "İkiyüz", "Üçyüz", "Dörtyüz", 

"Beşyüz", "Altıyüz", "Yediyüz", "Sekizyüz", "Dokuzyüz" };
                string[] a_thousands = { "", "Bin", "İkibin", "Üçbin", "Dörtbin", 

"Beşbin", "Altıbin", "Yedibin", "Sekizbin", "Dokuzbin" };
                string[] a_tenthousands = { "", "On", "Yirmi", "Otuz", "Kırk", 

"Elli", "Altmış", "Yetmiş", "Seksen", "Doksan" };
                string[] a_hundredthousands = { "", "Yüz", "İkiyüz", "Üçyüz", 

"Dörtyüz", "Beşyüz", "Altıyüz", "Yediyüz", "Sekizyüz", "Dokuzyüz" };
                string[] a_millions = { "", "Birmilyon", "İkimilyon", "Üçmilyon", 

"Dörtmilyon", "Beşmilyon", "Altımilyon", "Yedimilyon", "Sekizmilyon", "Dokuzmilyon" 

};

                lblText.Text = a_millions[millions] + " " + a_hundredthousands

[hundredthousands] + a_tenthousands[tenthousands] + " " + a_thousands[thousands] + "  

" + a_hundreds[hundreds] + " " + a_tens[tens] + " " + a_ones[ones];

            } 
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message.ToString());
            }
        }




        private void btnConvert_Click(object sender, EventArgs e)
        {
            NumberControl();
            ReadNumber();

        }

    }
}

【问题讨论】:

  • 您需要重新格式化您的代码以使其更具可读性:)
  • @enzglk,当我输入问题时,它没有格式化并且无处不在。现在好多了。
  • @enzglk:我为你重新格式化了你的代码。将来,您应该使用代码块(选择代码并按 Ctrl+K)。
  • 好的。很抱歉。我是这个网站的新手。非常感谢@Moo-and @Will

标签: c# .net text internationalization translation


【解决方案1】:
for (int j = 0; j < txtNumber.Text.Length; )  
{
    array[j] = (number / (10 ^ (txtNumber.Text.Length - (txtNumber.Text.Length - j)))) % 10;
    j += 1;
}

(txtNumber.Text.Length - (txtNumber.Text.Length - j)) 是 j,所以:

for (int j = 0; j < txtNumber.Text.Length; j += 1)  
{
    array[j] = (number / (10 ^ j)) % 10;
}

你正在与 j 异或 10?

此外,“Sekizmilyon”不是一个词。您需要在两者之间放置一个空格。

【讨论】:

  • 他可能想写Math.Pow。
  • 可能。虽然从全数开始并在每个步骤中将其向下移动通常效果更好,但您只需要在每个步骤中进行全除。
【解决方案2】:

看起来您正在尝试使用 ^ 计算 10 的幂,而您的意思可能是 Math.Pow。使用建议的缩减 aib,您的线路:

array[j] = (number / (10 ^ (txtNumber.Text.Length - (txtNumber.Text.Length - j)))) % 10;

变成:

array[j] = (number / (int)Math.Pow(10, j)) % 10;

其他几个建议:

  1. 将NumberControl 更改为返回布尔值,这样如果输入的数字太大,您可以跳过对ReadNumber 的调用。目前即使数字超过7位也会进入ReadNumber,导致数组越界错误

  2. 以if (txtSayi.Text.Length != 7) 开头的代码块似乎是多余的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 2014-04-17
    • 2012-12-19
    • 1970-01-01
    相关资源
    最近更新 更多