Skip to content

Instantly share code, notes, and snippets.

@DominicTobias
Last active Dec 31, 2020
Embed
What would you like to do?
import { round, floor, ceil, roundToTick, floorToTick, ceilToTick } from './math'
describe('math', () => {
it('round works up to 8 decimals and 999 million', () => {
expect(round(123.123456789, 0)).toEqual(123)
expect(round(123.123456789, 1)).toEqual(123.1)
expect(round(123.123456789, 2)).toEqual(123.12)
expect(round(123.123456789, 3)).toEqual(123.123)
expect(round(123.123456789, 4)).toEqual(123.1235)
expect(round(123.123456789, 5)).toEqual(123.12346)
expect(round(123.123456789, 6)).toEqual(123.123457)
expect(round(123.123456789, 7)).toEqual(123.1234568)
expect(round(123.123456789, 8)).toEqual(123.12345679)
expect(round(999999999.123456789, 8)).toEqual(999999999.12345679)
})
it('floor works up to 8 decimals and 999 million', () => {
expect(floor(123.123456789, 0)).toEqual(123)
expect(floor(123.123456789, 1)).toEqual(123.1)
expect(floor(123.123456789, 2)).toEqual(123.12)
expect(floor(123.123456789, 3)).toEqual(123.123)
expect(floor(123.123456789, 4)).toEqual(123.1234)
expect(floor(123.123456789, 5)).toEqual(123.12345)
expect(floor(123.123456789, 6)).toEqual(123.123456)
expect(floor(123.123456789, 7)).toEqual(123.1234567)
expect(floor(123.123456789, 8)).toEqual(123.12345678)
expect(floor(999999999.123456789, 8)).toEqual(999999999.12345679)
})
it('ceil works up to 8 decimals and 999 million', () => {
expect(ceil(123.123456789, 0)).toEqual(124)
expect(ceil(123.123456789, 1)).toEqual(123.2)
expect(ceil(123.123456789, 2)).toEqual(123.13)
expect(ceil(123.123456789, 3)).toEqual(123.124)
expect(ceil(123.123456789, 4)).toEqual(123.1235)
expect(ceil(123.123456789, 5)).toEqual(123.12346)
expect(ceil(123.123456789, 6)).toEqual(123.123457)
expect(ceil(123.123456789, 7)).toEqual(123.1234568)
expect(ceil(123.123456789, 8)).toEqual(123.12345679)
expect(ceil(123.123456789919, 8)).toEqual(123.12345679)
expect(ceil(999999999.123456789, 8)).toEqual(999999999.12345679)
})
it('roundToTick works up to 8 decimals and 999 million', () => {
expect(roundToTick(123, 0.1)).toEqual(123)
expect(roundToTick(123.1, 0.1)).toEqual(123.1)
expect(roundToTick(123.12, 0.1)).toEqual(123.1)
expect(roundToTick(123.156, 0.1)).toEqual(123.2)
expect(roundToTick(999999999.12345678, 0.1)).toEqual(999999999.1)
expect(roundToTick(999999999.15345678, 0.1)).toEqual(999999999.2)
expect(roundToTick(123, 0.05)).toEqual(123)
expect(roundToTick(123.1, 0.05)).toEqual(123.1)
expect(roundToTick(123.12, 0.05)).toEqual(123.1)
expect(roundToTick(123.156, 0.05)).toEqual(123.15)
expect(roundToTick(999999999.12345678, 0.05)).toEqual(999999999.1)
expect(roundToTick(999999999.15345678, 0.05)).toEqual(999999999.15)
expect(roundToTick(123, 1)).toEqual(123)
expect(roundToTick(123.1, 1)).toEqual(123)
expect(roundToTick(123.12, 1)).toEqual(123)
expect(roundToTick(123.5, 1)).toEqual(124)
expect(roundToTick(999999999.12345678, 1)).toEqual(999999999)
expect(roundToTick(999999999.9, 1)).toEqual(1000000000)
})
it('floorToTick works up to 8 decimals and 999 million', () => {
expect(floorToTick(123, 0.1)).toEqual(123)
expect(floorToTick(123.1, 0.1)).toEqual(123.1)
expect(floorToTick(123.12, 0.1)).toEqual(123.1)
expect(floorToTick(123.156, 0.1)).toEqual(123.1)
expect(floorToTick(999999999.12345678, 0.1)).toEqual(999999999.1)
expect(floorToTick(999999999.15345678, 0.1)).toEqual(999999999.1)
expect(floorToTick(123, 0.05)).toEqual(123)
expect(floorToTick(123.1, 0.05)).toEqual(123.1)
expect(floorToTick(123.12, 0.05)).toEqual(123.1)
expect(floorToTick(123.156, 0.05)).toEqual(123.15)
expect(floorToTick(999999999.12345678, 0.05)).toEqual(999999999.1)
expect(floorToTick(999999999.15345678, 0.05)).toEqual(999999999.15)
expect(floorToTick(123, 1)).toEqual(123)
expect(floorToTick(123.1, 1)).toEqual(123)
expect(floorToTick(123.12, 1)).toEqual(123)
expect(floorToTick(123.5, 1)).toEqual(123)
expect(floorToTick(999999999.12345678, 1)).toEqual(999999999)
expect(floorToTick(999999999.9, 1)).toEqual(999999999)
})
it('ceilToTick works up to 8 decimals and 999 million', () => {
expect(ceilToTick(123, 0.1)).toEqual(123)
expect(ceilToTick(123.1, 0.1)).toEqual(123.1)
expect(ceilToTick(123.12, 0.1)).toEqual(123.2)
expect(ceilToTick(123.156, 0.1)).toEqual(123.2)
expect(ceilToTick(999999999.12345678, 0.1)).toEqual(999999999.2)
expect(ceilToTick(999999999.15345678, 0.1)).toEqual(999999999.2)
expect(ceilToTick(123, 0.05)).toEqual(123)
expect(ceilToTick(123.1, 0.05)).toEqual(123.1)
expect(ceilToTick(123.12, 0.05)).toEqual(123.15)
expect(ceilToTick(123.156, 0.05)).toEqual(123.2)
expect(ceilToTick(999999999.12345678, 0.05)).toEqual(999999999.15)
expect(ceilToTick(999999999.15345678, 0.05)).toEqual(999999999.2)
expect(ceilToTick(123, 1)).toEqual(123)
expect(ceilToTick(123.1, 1)).toEqual(124)
expect(ceilToTick(123.12, 1)).toEqual(124)
expect(ceilToTick(123.5, 1)).toEqual(124)
expect(ceilToTick(999999999.12345678, 1)).toEqual(1000000000)
expect(ceilToTick(999999999.9, 1)).toEqual(1000000000)
})
})
export function round(value: number, decimals = 0) {
return Number(Math.round(Number(value + 'e' + decimals)) + 'e-' + decimals)
}
export function floor(value: number, decimals = 0) {
return Number(Math.floor(Number(value + 'e' + decimals)) + 'e-' + decimals)
}
export function ceil(value: number, decimals = 0) {
return Number(Math.ceil(Number(value + 'e' + decimals)) + 'e-' + decimals)
}
export function roundToTick(n: number, tickSize: number) {
return round(n * (1 / tickSize)) / (1 / tickSize)
}
export function floorToTick(n: number, tickSize: number) {
return floor(n * (1 / tickSize)) / (1 / tickSize)
}
export function ceilToTick(n: number, tickSize: number) {
return ceil(n * (1 / tickSize)) / (1 / tickSize)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment