很简单的一件事,我不知道为什么这么简单的程序都要问我,自己就不能上网查查么?好吧,我自己也写一个,虽然原始环境是C#,但是基本上做到了和语言无关(类C语言都可以,我没有使用任何系统函数,例如Math.Pow()等):
/// <summary>
/// 四舍五入函数
/// </summary>
/// <param name="d">待修剪数</param>
/// <param name="n">保留小数点后的位数</param>
/// <returns>返回修剪后的值</returns>
double Rounder(double d,int n)
{
double mi = 1;
//here does not use the Math.Pow() function.
//thus this function could be transfered to other language
//by small modification
for (int i = 1; i <= n; i++)
mi *= 10;
d = d * mi + 0.5;
long l = (long)d;
return l / mi;
}
如果作为类的成员的话,需要加上一些访问修饰符,例如private static什么的。
评论