如何在以下代码中舍入数字 [英] How to round off the numbers in the following code

查看:73
本文介绍了如何在以下代码中舍入数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string[] final = new string[result.Length];             

                for (int splitCount = 0; splitCount < result.Length; splitCount++)
                {
                    MatchCollection match = Regex.Matches(result[splitCount], @"\d+(\.\d+)?");

                    foreach (Match m in match)
                    {
                        final[splitCount] += (Double.Parse(m.Value, new System.Globalization.NumberFormatInfo() { NumberDecimalSeparator = "." })).ToString()+"            ";
                    }
                }









如何在上面的代码中将double值四舍五入为4位数





How to round off the double value to 4 digits in the above code

推荐答案

string[] final = new string[result.Length];             
for (int splitCount = 0; splitCount < result.Length; splitCount++)
{
  MatchCollection match = Regex.Matches(result[splitCount], @"\d+(?:\.\d*)?");
  final[splitCount] = string.Empty;      // Don't forget to initialize the entries in final.
  foreach (Match m in match)
  {
    final[splitCount] += Double.Parse(m.Value, System.Globalization.CultureInfo.InvariantCulture).ToString("F4")+"            ";
  }
}



只需使用.ToString()中的格式信息来控制精度。

I更改了正则表达式,以便它是一个非捕获组并允许小数点而不跟随数字。例如,1234。


Just use the format info in the .ToString() to control the precision.
I changed the regex so that it was a noncapturing group and to allow for decimal point without following digits. E.g., "1234."


使用 Math.Round [ ^ ]功能。


int round(双号)

{

返回(数字> = 0)? (int)(数字+ 0.5):( int)(数字 - 0.5);

}







使用Long而不是Int如果你有很长的否



另一种方式是



double a = 2.333333;

System.Math.Round(a)

使用数学函数
int round(double number)
{
return (number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5);
}



Use Long Instead Of Int If You have A long No

And Another Way Is

double a = 2.333333;
System.Math.Round(a)
Use Math Funtion


这篇关于如何在以下代码中舍入数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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