Skip to content

Commit

Permalink
feat: 新增 round, roundUp, roundDown
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed May 23, 2019
1 parent 7fe8bf7 commit 91cfa4b
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './base64Encode'
export * from './base64UrlDecode'
export * from './base64UrlEncode'
export * from './bindEvent'
export * from './calculateScore'
export * from './castArray'
export * from './chunk'
export * from './clamp'
Expand Down Expand Up @@ -78,6 +79,7 @@ export * from './range'
export * from './reduce'
export * from './repeat'
export * from './request'
export * from './round'
export * from './sample'
export * from './shuffle'
export * from './sleep'
Expand Down
52 changes: 52 additions & 0 deletions src/round.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @see https://stackoverflow.com/a/12830454
* @see https://github.com/sindresorhus/round-to/blob/master/index.js
* @see https://github.com/lodash/lodash/blob/master/.internal/createRound.js
* @see https://blog.caaat.xyz/archives/math_round.html
*/
function nativeRound(fn: Math['round'] | Math['ceil'] | Math['floor'], number: number, precision: number = 0): number {
if (number < 0 && fn === Math.round) {
return -nativeRound(fn, -number, precision)
}
precision = precision >= 0 ? Math.min(precision, 292) : Math.max(precision, -292)
if (precision) {
let pair = `${number}e`.split('e')
const value = fn(+`${pair[0]}e${+pair[1] + precision}`)
pair = `${value}e`.split('e')
return +`${pair[0]}e${+pair[1] - precision}`
}
return fn(number)
}

/**
* 对传入的数字按给定的精度四舍五入后返回。
*
* @param number 传入的数字
* @param precision 精度
* @returns 返回结果
*/
export function round(number: number, precision: number = 0) {
return nativeRound(Math.round, number, precision)
}

/**
* 对传入的数字按给定的精度向上取值后返回。
*
* @param number 传入的数字
* @param precision 精度
* @returns 返回结果
*/
export function roundUp(number: number, precision: number = 0) {
return nativeRound(Math.ceil, number, precision)
}

/**
* 对传入的数字按给定的精度向下取值后返回。
*
* @param number 传入的数字
* @param precision 精度
* @returns 返回结果
*/
export function roundDown(number: number, precision: number = 0) {
return nativeRound(Math.floor, number, precision)
}
34 changes: 34 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1807,3 +1807,37 @@ describe('ii', () => {
)
})
})

describe('round', () => {
test('round', () => {
typedExpectEqual(vtils.round(0.129, 3), 0.129)
typedExpectEqual(vtils.round(0.129, 2), 0.13)
typedExpectEqual(vtils.round(0.129, 1), 0.1)
typedExpectEqual(vtils.round(1.005, 2), 1.01)
typedExpectEqual(vtils.round(1.005, 0), 1)
typedExpectEqual(vtils.round(111.1, -2), 100)
typedExpectEqual(vtils.round(-0.375, 2), -0.38)
typedExpectEqual(Number.isNaN(vtils.round(10000000000000, 8)), false)
typedExpectEqual(vtils.round(0.37542323423423432432432432432, 8), 0.37542323)
})

test('roundUp', () => {
typedExpectEqual(vtils.roundUp(0.111, 3), 0.111)
typedExpectEqual(vtils.roundUp(0.111, 2), 0.12)
typedExpectEqual(vtils.roundUp(0.111, 1), 0.2)
typedExpectEqual(vtils.roundUp(1.004, 2), 1.01)
typedExpectEqual(vtils.roundUp(1.111, 0), 2)
typedExpectEqual(vtils.roundUp(111.1, -2), 200)
typedExpectEqual(vtils.roundUp(-0.375, 2), -0.37)
})

test('roundDown', () => {
typedExpectEqual(vtils.roundDown(0.666, 3), 0.666)
typedExpectEqual(vtils.roundDown(0.666, 2), 0.66)
typedExpectEqual(vtils.roundDown(0.666, 1), 0.6)
typedExpectEqual(vtils.roundDown(1.006, 2), 1.0)
typedExpectEqual(vtils.roundDown(1.006, 0), 1)
typedExpectEqual(vtils.roundDown(111.6, -2), 100)
typedExpectEqual(vtils.roundDown(-0.375, 2), -0.38)
})
})

0 comments on commit 91cfa4b

Please sign in to comment.