Skip to content

Commit

Permalink
feat(useRound): new function (#1820)
Browse files Browse the repository at this point in the history
  • Loading branch information
webfansplz committed Jul 10, 2022
1 parent 80461b4 commit 62a6056
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
16 changes: 16 additions & 0 deletions 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)
```
15 changes: 15 additions & 0 deletions 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)
})
})
15 changes: 15 additions & 0 deletions 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<number>): ComputedRef<number> {
return computed<number>(() => Math.round(resolveUnref(value)))
}

0 comments on commit 62a6056

Please sign in to comment.