首页 > 解决方案 > C#如何通过多次停止小数移位

问题描述

我创建了一个表格来安排船舱。

在此处输入图像描述

问题:多次返回时,小数点又移动了两个空格。我如何防止它进一步移动?

代码片段:

private void btnGetInv_Click(object sender, EventArgs e)
{

    //if this is a ship hold, roll percentile to get how much of the ship's total weight capacity is being carried.
    if(ShipHold==true)
    {
        decimal Percentage = 0;
        decimal HoldWeight = 0;
        LoNum = 1;
        HiNum = 100;
        DieResult = 0;

        Percentage = DieResult + 1 + RollDice.Next(LoNum - 1, HiNum);
        WeightAvail = Percentage / 100 * WeightAvail;
        HoldWeight = WeightAvail;
        RTBItems.Text = "percentage of weight = " + HoldWeight + "\r\n" + RTBItems.Text;
    }

WeightAvail也是一个decimal。然而; LoNum, HiNum, 和DieResult都是int使用该Random.Next函数的类型。如果我可以四舍五入到最接近的整数可能是最好的。我正在使用 Visual Studio 2019。

这是我的修复:

HoldWeight = Percentage / 100 * WeightAvail;
//HoldWeight = WeightAvail;
RTBItems.Text = "percentage of weight = " + HoldWeight + "\r\n" + RTBItems.Text;

HoldWeight = 0;

标签: c#

解决方案


推荐阅读