0

Visual Studio 2010、Windows フォーム (c#) を使用しています。

numericUpDown1 を使用して、テキスト ボックスの値の小数点以下の桁数を変更する必要があります。

例:

ここに画像の説明を入力 ここに画像の説明を入力

ここに画像の説明を入力 ここに画像の説明を入力

ここに画像の説明を入力 ここに画像の説明を入力

ここに画像の説明を入力 ここに画像の説明を入力

ここに画像の説明を入力 ここに画像の説明を入力

私はこのコードを試しました:

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {

      //  tbxConvertito.Text = (decimal.Parse(tbxConvertito.Text) * 10m).ToString();

        if (numericUpDown1.Value == 0)
        {
            int decimalPlace = 0;
           // Decimal xDecimal = decimal.Parse(tbxConvertito.text);

            decimal xDecimal = decimal.Parse(tbxConvertito.Text);
            tbxConvertito.Text = (Math.Round(xDecimal, decimalPlace)).ToString();
        }

        if (numericUpDown1.Value == 1)
        {
            int decimalPlace = 1;
            // Decimal xDecimal = decimal.Parse(tbxConvertito.text);

            decimal xDecimal = decimal.Parse(tbxConvertito.Text);
            tbxConvertito.Text = (Math.Round(xDecimal, decimalPlace)).ToString();
        }

    }

しかし、動作しません。どうすればこれを解決できますか?

4

2 に答える 2

2

元の値を維持するには、10進値でフィールドを作成する必要があります。

あなたがそれをするなら:

int decimalPlace = 1;
decimal xDecimal = decimal.Parse(tbxConvertito.Text);
tbxConvertito.Text = (Math.Round(xDecimal, decimalPlace)).ToString();

10進変数の元の値が失われます。

これを試して:

public partial class Form1 : Form
{
        public decimal myDecimal = 3755.25012345M;

        public Form1()
        {
            InitializeComponent();

            tbxConvertito.Text = myDecimal.ToString();

            numericUpDown1_ValueChanged(this, EventArgs.Empty);
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {          
            int decimalPlace = (int)numericUpDown1.Value;
            tbxConvertito.Text = Decimal.Round(myDecimal, decimalPlace).ToString();
        }
}

使用Round方法なしの解決策:

  public partial class Form1 : Form
    {
        public decimal myDecimal = 3755.25012345M;

        public Form1()
        {
            InitializeComponent();

            tbxConvertito.Text = myDecimal.ToString();

            numericUpDown1_ValueChanged(this, EventArgs.Empty);
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {          
            int decimalPlace = (int)numericUpDown1.Value;

            string[] numbers = myDecimal.ToString().Split(new char[] { '.', ',' });

            string tmp = string.Empty;

            if (decimalPlace <= numbers[1].Length)
            {
                tmp = "," + numbers[1].Substring(0, decimalPlace);

                if (tmp.EndsWith(","))
                    tmp = string.Empty;
            }
            else
                tmp = "," + numbers[1];

            tbxConvertito.Text = numbers[0] + tmp;
        }
    }
于 2013-01-19T12:42:41.563 に答える
0

これは、操作を使用して実現できますString。これを試してみてください

decimal xDecimal = decimal.Parse(tbxConvertito.Text);
string str = xDecimal.ToString();
if (!str.Contains(","))
            decimalPlace--;
tbxConvertito.Text = str.Replace(",", "").Insert((str.Length-1) - decimalPlace,",");

そして、あなたのコードでこのようにそれを使用してください

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{

  //  tbxConvertito.Text = (decimal.Parse(tbxConvertito.Text) * 10m).ToString();

    if (numericUpDown1.Value == 0)
    {
        int decimalPlace = 0;
       // Decimal xDecimal = decimal.Parse(tbxConvertito.text);

        decimal xDecimal = decimal.Parse(tbxConvertito.Text);
        string str = xDecimal.ToString();
        if (!str.Contains(","))
            decimalPlace--;
        tbxConvertito.Text = str.Replace(",", "").Insert((str.Length-1) - decimalPlace,",");
    }

    if (numericUpDown1.Value == 1)
    {
        int decimalPlace = 1;
        // Decimal xDecimal = decimal.Parse(tbxConvertito.text);

        decimal xDecimal = decimal.Parse(tbxConvertito.Text);
        string str = xDecimal.ToString();
        if (!str.Contains(","))
            decimalPlace--;
        tbxConvertito.Text = str.Replace(",", "").Insert((str.Length-1) - decimalPlace,",");
    }

}
于 2013-01-19T12:44:07.303 に答える