Answers:
/**
 * @param num The number to round
 * @param precision The number of decimal places to preserve
 */
function roundUp(num, precision) {
  precision = Math.pow(10, precision)
  return Math.ceil(num * precision) / precision
}
roundUp(192.168, 1) //=> 192.2ceil()它将为您提供193,因此我们必须确保要保留的所有精度都在小数点之前。然后,我们进行逆运算以恢复“原始”值。
                    192.19999999999997,你可以申请.toFixed(1)到num
                    有点晚了,但是可以为此目的创建可重用的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 =)
price.toFixed(2)
                    OP预计有两件事:
 A.向上取整至十分之一,
 B。在百分数处显示零(货币的典型需求)。
满足这两个要求似乎需要针对上述每个方法使用单独的方法。这是一种基于suryakiran建议答案的方法:
//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重要说明:此解决方案使用负数和指数产生的结果截然不同。
为了比较这个答案和两个非常相似的答案,请参阅以下两种方法。通常,第一个简单地四舍五入到最接近的百分之一,第二个简单地四舍五入到最接近的百分之一(更大)。
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此函数限制无小数的小数
function limitDecimal(num,decimal){
     return num.toString().substring(0, num.toString().indexOf('.')) + (num.toString().substr(num.toString().indexOf('.'), decimal+1));
}我已经使用@AndrewMarshall回答很长时间了,但是发现了一些极端情况。以下测试未通过:
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
roundUp(37.760000000000005, 4)应该是37.7601,roundUp(5.83333333, 4)应该是5.8334。对于我提供的fn,这两个(也是您的第一个)都成立。