使用numericUpDown更改文本框中的小数位 [英] change decimal place in textbox using numericUpDown

查看:522
本文介绍了使用numericUpDown更改文本框中的小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio 2010,Windows窗体(c#).

Im using Visual Studio 2010, windows form (c#).

我需要使用numericUpDown1在文本框中更改值的小数位.

I need change decimals place of value in textbox using 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();
        }

    }

但不起作用.请问我该如何解决?

but not work. How can I solve this, please?

推荐答案

您应创建带有十进制值的字段,以保留原始值.

You should create field with decimal value, to keep original value.

如果您这样做:

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

您丢失了十进制变量的原始值.

You lose original value of your decimal variable.

尝试一下:

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方法的解决方案:

Solution without use Round method:

  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;
        }
    }

这篇关于使用numericUpDown更改文本框中的小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆