ceilToStep function
Rounds a number up to a multiple of a step, without float noise.
ts
import { ceilToStep } from 'utils';Signature
ts
export function ceilToStep(value: number, step = 1): numberParameters
| Name | Description |
|---|---|
value | The number to round up. |
step | The step, a positive number. Defaults to 1. |
Returns
The smallest multiple of step greater than or equal to value.
Example
ts
ceilToStep(0.31, 0.1); // 0.4
ceilToStep(-7, 5); // -5Tests
Line coverage: 100 %.
- ✅ ceils 0.3 to step 0.1 as 0.3
- ✅ ceils 0.31 to step 0.1 as 0.4
- ✅ ceils 7 to step 5 as 10
- ✅ ceils -7 to step 5 as -5
- ✅ ceils 0.7 to step 0.1 as 0.7
- ✅ does not return -0
- ✅ takes the defaults
Source
src/math/ceil-to-step.ts (on GitHub)
Files it needs
src/math/ceil-to-step.ts imports, directly or not:
Source code
ts
import { snapToStep } from './internal';
/**
* Rounds a number up to a multiple of a step, without float noise.
*
* @param value - The number to round up.
* @param step - The step, a positive number. Defaults to `1`.
* @returns The smallest multiple of `step` greater than or equal to `value`.
* @example
* ceilToStep(0.31, 0.1); // 0.4
* ceilToStep(-7, 5); // -5
*/
export function ceilToStep(value: number, step = 1): number {
return snapToStep(value, step, Math.ceil);
}Test file
ts
import { ceilToStep } from './ceil-to-step';
describe(ceilToStep, () => {
it.for([
[0.3, 0.1, 0.3],
[0.31, 0.1, 0.4],
[7, 5, 10],
[-7, 5, -5],
[0.7, 0.1, 0.7],
] as const)('ceils %s to step %s as %s', ([value, step, expected]) => {
expect(ceilToStep(value, step)).toBe(expected);
});
it('does not return -0', () => {
expect(Object.is(ceilToStep(-0.4, 1), 0)).toBe(true);
});
it('takes the defaults', () => {
expect(ceilToStep(2.3)).toStrictEqual(ceilToStep(2.3, 1));
});
});Keywords: ceil, to, step, math, function