在C#中舍入十进制值

Gud*_*ddu 7 c# numbers decimal rounding

我如何舍入小数值?
示例:

十进制值="19500.98"

我需要将此值显示到文本框中,并将其舍入为"19501"

如果十进制值="19500.43"

然后

value ="19500"

Jon*_*eet 22

看看Math.Round(decimal)带有MidpointRounding参数的重载.

当然,您需要解析并格式化值以将其从/到文本.如果这是用户输入的输入,您应该使用decimal.TryParse返回值来确定输入是否有效.

string text = "19500.55";
decimal value;
if (decimal.TryParse(text, out value))
{
    value = Math.Round(value);
    text = value.ToString();
    // Do something with the new text value
}
else
{
    // Tell the user their input is invalid
}
Run Code Online (Sandbox Code Playgroud)


Pau*_*der 5

Math.Round(值,0)