|
| 1 | +/** |
| 2 | + * Lodash数字相关操作 |
| 3 | + * @private |
| 4 | + */ |
| 5 | + |
| 6 | +const _ = require('lodash') |
| 7 | + |
| 8 | +// inRange: 判断是否在范围中 |
| 9 | +console.log(_.inRange(3, 2, 4)) |
| 10 | +// => true |
| 11 | +console.log(_.inRange(4, 8)) |
| 12 | +// => true |
| 13 | +console.log(_.inRange(4, 2)) |
| 14 | +// => false |
| 15 | +console.log(_.inRange(2, 2)) |
| 16 | +// => false |
| 17 | +console.log(_.inRange(1.2, 2)) |
| 18 | +// => true |
| 19 | +console.log(_.inRange(5.2, 4)) |
| 20 | +// => false |
| 21 | +console.log(_.inRange(-3, -2, -6)) |
| 22 | +// => true |
| 23 | + |
| 24 | + |
| 25 | +// random: 返回指定范围随机值 |
| 26 | +console.log(_.random(0, 5)) |
| 27 | +// => an integer between 0 and 5 |
| 28 | +console.log(_.random(5)) |
| 29 | +// => also an integer between 0 and 5 |
| 30 | +console.log(_.random(5, true)) |
| 31 | +// => a floating-point number between 0 and 5 |
| 32 | +console.log(_.random(1.2, 5.2)) |
| 33 | +// => a floating-point number between 1.2 and 5.2 |
| 34 | + |
| 35 | +// add : 相加 |
| 36 | +console.log(_.add(6, 4)) |
| 37 | +// => 10 |
| 38 | + |
| 39 | + |
| 40 | +// divide: 相除 |
| 41 | +console.log(_.divide(6, 4)) |
| 42 | +// => 1.5 |
| 43 | + |
| 44 | + |
| 45 | +// multiply: 相乘 |
| 46 | +console.log(_.multiply(6, 4)) |
| 47 | +// => 24 |
| 48 | + |
| 49 | + |
| 50 | +// subtract: 相减 |
| 51 | +_.subtract(6, 4) |
| 52 | +// => 2 |
| 53 | + |
| 54 | +// sum: 求和 |
| 55 | +_.sum([4, 2, 8, 6]) |
| 56 | +// => 20 |
| 57 | + |
| 58 | + |
| 59 | +// ceil: 向上取整 |
| 60 | +console.log(_.ceil(4.006)) |
| 61 | +// => 5 |
| 62 | +console.log(_.ceil(6.004, 2)) |
| 63 | +// => 6.01 |
| 64 | +console.log(_.ceil(6040, -2)) |
| 65 | +// => 6100 |
| 66 | + |
| 67 | +// floor: 向下取整 |
| 68 | +console.log(_.floor(4.006)) |
| 69 | +// => 4 |
| 70 | +console.log(_.floor(0.046, 2)) |
| 71 | +// => 0.04 |
| 72 | +console.log(_.floor(4060, -2)) |
| 73 | +// => 4000 |
| 74 | + |
| 75 | + |
| 76 | +// round: 四舍五入 |
| 77 | +console.log(_.round(4.006)) |
| 78 | +// => 4 |
| 79 | +console.log(_.round(4.006, 2)) |
| 80 | +// => 4.01 |
| 81 | +console.log(_.round(4060, -2)) |
| 82 | +// => 4100 |
| 83 | + |
| 84 | + |
| 85 | +// max: 最大值 |
| 86 | +console.log(_.max([4, 2, 8, 6])) |
| 87 | +// => 8 |
| 88 | +console.log(_.max([])) |
| 89 | +// => undefined |
| 90 | + |
| 91 | + |
| 92 | +// mean: 求平均值 |
| 93 | +console.log(_.mean([4, 2, 8, 6])) |
| 94 | +// => 5 |
| 95 | + |
| 96 | + |
| 97 | +// min: 最小值 |
| 98 | +console.log(_.min([4, 2, 8, 6])) |
| 99 | +// => 2 |
| 100 | +console.log(_.min([])) |
| 101 | +// => undefined |
0 commit comments