日期:2014-05-16 浏览次数:20327 次
Object.extend(Number.prototype, { add: function (num) { var reg = /\./i; var me = this; if (!reg.test(me) && !reg.test(num)) { return me += num; } var i1, i2; try { i1 = me.toString().split('.')[1].length; } catch (e) { i1 = 0; } try { i2 = num.toString().split('.')[1].length; } catch (e) { i2 = 0; } var m = Math.pow(10, Math.max(i1, i2)); return (me * m + num * m) / m; }, sub: function (num) { var reg = /\./i; var me = this; if (!reg.test(me) && !reg.test(num)) { return me -= num; } var i1, i2; try { i1 = me.toString().split('.')[1].length; } catch (e) { i1 = 0; } try { i2 = num.toString().split('.')[1].length; } catch (e) { i2 = 0; } var m = Math.pow(10, Math.max(i1, i2)); var n = (i1 >= i2) ? i1 : i2; return ((me * m - num * m) / m).toFixed(n); } });