【www.gdgbn.com--ling】

round:通过将 point 值舍入到最接近的整数值,将指定的 pointf 转换为point 对象。
请看下面的例子:

   math.round(3.44, 1); //returns 3.4.  四舍

   math.round(3.451, 1); //returns 3.5  五后非零就进一
   math.round(3.45, 1); //returns 3.4. 五后皆零看奇偶, 五前为偶应舍 去

   math.round(3.75, 1);  //returns 3.8  五后皆零看奇偶,五前为奇要进一
   math.round(3.46, 1); //returns 3.5. 六入

 

  如果要实现我们传统的四舍五入的功能,一种比较简单,投机的方法就是在数的后面加上0.0000000001,很小的一个数.因为"五后非零就进一", 所以可以保证5一定进一.

  当然也可以自己写函数, 下面给出一段代码:

public static decimal unit = 0.0.1m

static public  decimal  round(decimal d)

{

    return round(d,unit)

}

 

static public decimal round(decimal d,decimal unit)

{

   decimal rm = d % unit;

   decimal result = d-rm;

   if( rm >= unit /2)

  {

      result += unit;

  } 

  return result ;

}

请注意round是一个比它强大得多似乎仅仅是因为它可以全面为十进制场所的具体数量。所有其他轮零小数始终。例如:

n = 3.145;
a = system.math.round (n, 2, midpointrounding.toeven);       // 3.14
b = system.math.round (n, 2, midpointrounding.awayfromzero); // 3.15

 

 

truncate:实质上舍去小数部分并向0方向靠拢,比如坐标0.9和-0.9都变为0。

ceiling:向下一个最大的整数靠拢,如0.9变为1,-0.9变为0

与其他功能,您必须使用乘/除弄虚作假,以达到同样的效果:

c = system.math.truncate (n * 100) / 100;                    // 3.14
d = system.math.ceiling (n * 100) / 100;                     // 3.15

 

本文来源:http://www.gdgbn.com/shujuku/28685/