如何将十进制值四舍五入为最接近的最高位数

问题描述 投票:-1回答:3

例如:13.799〜13.80,13.023〜13.02,27.8555〜27.86此上存在的任何算法

c# asp.net
3个回答
1
投票
查看Math.Round(decimal)或带有MidpointRounding参数的重载。

Math.Round(value)

另一个选择是使用decimal.Ceiling(13.023)

0
投票
在最新版本的XPath中,您可以使用round(., 2)舍入到2个十进制数字。

0
投票
如Nouman所建议的,Math.Round(value)是正确的选择。

在您需要时,您可以使用以下方式:

public static void Main() { //Array where the Information is stored double[] values = { 13.799, 13.023, 27.8555 }; //for each value in the array foreach (double value in values) //write it to the console like unrounded-value => rounded-value //specify on how many Digit you want to round Console.WriteLine("{0} => {1}", value, Math.Round(value, 2)); }

该示例显示以下输出:

13.799 => 13.80 13.023 => 13.02 27.8555 => 27.86

附加说明:

[十进制的值四舍五入为4而不是5,因为此重载使用默认的ToEven约定。

© www.soinside.com 2019 - 2024. All rights reserved.