将NumericUpDown中的数值减半或加倍

本文关键字:NumericUpDown | 更新日期: 2023-09-27 18:12:23

我在VS 2015中创建了两个c#按钮。

我该如何减少数值,并将数值翻倍,位于NumericUpDown类与按钮?

 /// <summary>
    /// place single bet, Low
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button4_Click(object sender, EventArgs e)
    {
        CurrentSite.amount =((decimal)nudApiBet.Value);
        CurrentSite.chance = (decimal)(nudApiChance.Value);
        CurrentSite.PlaceBet(false, (decimal)nudApiBet.Value,(decimal)(nudApiChance.Value));
    }
    private void nudApiBet_ValueChanged(object sender, EventArgs e)
    {
        if ((sender as NumericUpDown).Name == "nudApiBet")
        {
            lblApiBetProfit.Text = ((nudApiBet.Value * nudApiPayout.Value) - nudApiBet.Value).ToString("0.00000000"); 
        }
        else if ((sender as NumericUpDown).Name == "nudApiChance")
        {
            decimal payout = (100m - CurrentSite.edge) / (nudApiChance.Value);
            if (nudApiPayout.Value != payout)
                nudApiPayout.Value = payout;
            lblApiBetProfit.Text = ((nudApiBet.Value * payout) - nudApiBet.Value).ToString("0.00000000"); 
        }
        else if ((sender as NumericUpDown).Name == "nudApiPayout")
        {
            decimal chance = (100m - CurrentSite.edge) / (nudApiPayout.Value);
            if (nudApiChance.Value != chance)
                nudApiChance.Value = chance;
            lblApiBetProfit.Text = ((nudApiBet.Value * nudApiPayout.Value) - nudApiBet.Value).ToString("0.00000000"); 
        }
    }

将NumericUpDown中的数值减半或加倍

要使NumericUpDownValue减半或加倍,只需将Value除以2并将结果赋值给Value即可。

但是您看到的结果将基于DecimalPlaces的值进行舍入。该值将包含实际结果,但文本显示四舍五入的结果。为了提高文本的准确性,增加DecimalPlaces并将其设置为2或更高。

numericUpDown1.Value /= 2;     // numericUpDown1.Value = numericUpDown1.Value / 2;
numericUpDown1.Value *= 2;     // numericUpDown1.Value = numericUpDown1.Value * 2;

假设:

1)导航到表单的设计页面。

2)双击按钮,或导航到控件属性窗口中的Events选项卡,找到"click"事件,双击它(属性窗口应该在您的右侧,并包含一个闪电,这是事件选项卡)。这会自动生成如下代码:

private void btnYourButton(object sender, EventArgs e)
        {
         //this is where your code goes
        }

3)在{}括号内编写执行所需函数的代码。比如:

numericUpDown.Value = numericUpDown.Value * 2;