根据precision(精度)向上舍入number

/**
 * 根据precision(精度)向上舍入number。(precision(精度)可以理解为保留几位小数)
 * Computes `number` rounded up to `precision`
 * @param {number} number The number to round up
 * @param {number} [precision=0] The precision to round up to
 * @returns {number} Returns the rounded up number
 * @example
 * ceil(4.006)
 * // => 5
 * ceil(6.004, 2)
 * // => 6.01
 */

import createRound from "../utils/createRound"

var ceil = createRound("ceil")

export default ceil
/**
 * Creates a function like `round`
 * @param {string} methodName The name of the `Math` method to use when rounding
 * @returns {Function} Returns the new round function
 */

import toNumber from "./toNumber" //在我的博客中搜索【转换为数字】
import toInteger from "./toInteger" //在我的博客中搜索【转换为整数】

function createRound(methodName) {
  var func = Math[methodName]
  return function(number, precision) {
    number = toNumber(number)
    precision = precision == null ? 0 : Math.min(toInteger(precision), 292)
    if (precision) {
      // Shift with exponential notation to avoid floating-point issues.
      // See [MDN](https://mdn.io/round#Examples) for more details.
      var pair = (String(number) + "e").split("e"),
        value = func(pair[0] + "e" + (+pair[1] + precision))

      pair = (String(value) + "e").split("e")
      return +(pair[0] + "e" + (+pair[1] - precision))
    }
    return func(number)
  }
}

export default createRound

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值