Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useRound): new function (#1820)
- Loading branch information
1 parent
80461b4
commit 62a6056
Showing
3 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<number>): ComputedRef<number> { | ||
return computed<number>(() => Math.round(resolveUnref(value))) | ||
} |