如何在Javascript中收集数字?

我想用Javascript来收集一个数字。 由于数字是货币,所以我希望它能像这些例子(小数点后两位)凑齐:

  • 192.168 => 192.20
  • 192.11 => 192.20
  • 192.21 => 192.30
  • 192.26 => 192.30
  • 192.20 => 192.20

如何实现这个使用Javascript? 内置的Javascript函数将基于标准逻辑(less于5个以上)整理数字。

// precision is 10 for 10ths, 100 for 100ths, etc. function roundUp(num, precision) { return Math.ceil(num * precision) / precision } roundUp(192.168, 10) //=> 192.2 

有点迟了,但是,可以为此创build一个可重用的JavaScript函数:

 // Arguments: number to round, number of decimal places function roundNumber(rnum, rlength) { var newnumber = Math.round(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength); return newnumber; } 

调用该函数

 alert(roundNumber(192.168,2)); 

正常的四舍五入将有一个小小的调整:

 Math.round(price * 10)/10 

如果要保留货币格式,则可以使用Number方法.toFixed()

 (Math.round(price * 10)/10).toFixed(2) 

虽然这会使它成为String =)

非常接近TheEye答案,但我改变了一点点,使其工作:

 <script type="text/javascript"> var num = 192.16; num = Math.ceil(num * 10) / 10; alert(num); </script> 

好吧,这已经得到了回答,但我想你可能会喜欢看到我的答案调用math.pow()函数一次。 我想我喜欢保持干爽。

 function roundIt(num, precision) { var rounder = Math.pow(10, precision); return (Math.round(num * rounder) / rounder).toFixed(precision) }; 

这种把它放在一起。 将Math.round()replace为Math.ceil()来取整,而不是取整,这是OP想要的。

OP期待两件事情:
答:要高到十分之一,而且
B.在百分之一位置显示一个零(一个典型的货币需求)。

满足这两个要求似乎需要上述每一个单独的方法。 这是一个build立在suryakiran的build议答案上的方法:

 //Arguments: number to round, number of decimal places. function roundPrice(rnum, rlength) { var newnumber = Math.ceil(rnum * Math.pow(10, rlength-1)) / Math.pow(10, rlength-1); var toTenths = newnumber.toFixed(rlength); return toTenths; } alert(roundPrice(678.91011,2)); // returns 679.00 alert(roundPrice(876.54321,2)); // returns 876.60 

重要提示:这个解决scheme会产生一个非常不同的结果,包括负数和指数。

为了比较这个答案和两个非常相似的答案,请看以下两种方法。 第一个简单的四舍五入到最接近的百分之一,第二个简单地舍入到最接近的第一百(更大)。

 function roundNumber(rnum, rlength) { var newnumber = Math.round(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength); return newnumber; } alert(roundNumber(678.91011,2)); // returns 678.91 function ceilNumber(rnum, rlength) { var newnumber = Math.ceil(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength); return newnumber; } alert(ceilNumber(678.91011,2)); // returns 678.92 

我一直在使用@AndrewMarshall的答案,但是发现了一些边缘情况。 以下testing未通过:

 equals(roundUp(9.69545, 4), 9.6955); equals(roundUp(37.760000000000005, 4), 37.76); equals(roundUp(5.83333333, 4), 5.8333); 

这里是我现在用来收集行为正确的:

 // Closure (function() { /** * Decimal adjustment of a number. * * @param {String} type The type of adjustment. * @param {Number} value The number. * @param {Integer} exp The exponent (the 10 logarithm of the adjustment base). * @returns {Number} The adjusted value. */ function decimalAdjust(type, value, exp) { // If the exp is undefined or zero... if (typeof exp === 'undefined' || +exp === 0) { return Math[type](value); } value = +value; exp = +exp; // If the value is not a number or the exp is not an integer... if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) { return NaN; } // If the value is negative... if (value < 0) { return -decimalAdjust(type, -value, exp); } // Shift value = value.toString().split('e'); value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp))); // Shift back value = value.toString().split('e'); return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp)); } // Decimal round if (!Math.round10) { Math.round10 = function(value, exp) { return decimalAdjust('round', value, exp); }; } // Decimal floor if (!Math.floor10) { Math.floor10 = function(value, exp) { return decimalAdjust('floor', value, exp); }; } // Decimal ceil if (!Math.ceil10) { Math.ceil10 = function(value, exp) { return decimalAdjust('ceil', value, exp); }; } })(); // Round Math.round10(55.55, -1); // 55.6 Math.round10(55.549, -1); // 55.5 Math.round10(55, 1); // 60 Math.round10(54.9, 1); // 50 Math.round10(-55.55, -1); // -55.5 Math.round10(-55.551, -1); // -55.6 Math.round10(-55, 1); // -50 Math.round10(-55.1, 1); // -60 Math.round10(1.005, -2); // 1.01 -- compare this with Math.round(1.005*100)/100 above Math.round10(-1.005, -2); // -1.01 // Floor Math.floor10(55.59, -1); // 55.5 Math.floor10(59, 1); // 50 Math.floor10(-55.51, -1); // -55.6 Math.floor10(-51, 1); // -60 // Ceil Math.ceil10(55.51, -1); // 55.6 Math.ceil10(51, 1); // 60 Math.ceil10(-55.59, -1); // -55.5 Math.ceil10(-59, 1); // -50 

来源: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round

此function限制小数点无圆整数

 function limitDecimal(num,decimal){ return num.toString().substring(0, num.toString().indexOf('.')) + (num.toString().substr(num.toString().indexOf('.'), decimal+1)); }