diff --git a/packages/math/useRound/index.md b/packages/math/useRound/index.md new file mode 100644 index 00000000000..61881590193 --- /dev/null +++ b/packages/math/useRound/index.md @@ -0,0 +1,16 @@ +--- +category: '@Math' +--- + +# useRound + +Reactively Math.round(value). + +## Usage + +```ts +import { useRound } from '@vueuse/math' + +const value = ref(20.49) +const result = useRound(value) +``` diff --git a/packages/math/useRound/index.test.ts b/packages/math/useRound/index.test.ts new file mode 100644 index 00000000000..42e89a634cb --- /dev/null +++ b/packages/math/useRound/index.test.ts @@ -0,0 +1,15 @@ +import { ref } from 'vue-demi' +import { useRound } from '.' + +describe('useRound', () => { + it('should be defined', () => { + expect(useRound).toBeDefined() + }) + it('should work', () => { + const base = ref(20.49) + const result = useRound(base) + expect(result.value).toBe(20) + base.value = -20.51 + expect(result.value).toBe(-21) + }) +}) diff --git a/packages/math/useRound/index.ts b/packages/math/useRound/index.ts new file mode 100644 index 00000000000..c184dc055dc --- /dev/null +++ b/packages/math/useRound/index.ts @@ -0,0 +1,15 @@ +import type { ComputedRef } from 'vue-demi' +import { computed } from 'vue-demi' +import type { MaybeComputedRef } from '@vueuse/shared' +import { resolveUnref } from '@vueuse/shared' + +/** + * Reactively Math.round(value). + * + * @see https://vueuse.org/useRound + * @param base + * @param exponent + */ +export function useRound(value: MaybeComputedRef): ComputedRef { + return computed(() => Math.round(resolveUnref(value))) +}