Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
85 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,24 @@ | ||
--- | ||
category: '@Math' | ||
--- | ||
|
||
# usePrecision | ||
|
||
Reactively set the precision of a number. | ||
|
||
## Usage | ||
|
||
```ts | ||
import { usePrecision } from '@vueuse/math' | ||
|
||
const value = ref(3.1415) | ||
const result = usePrecision(value, 2) // 3.14 | ||
|
||
const ceilResult = usePrecision(value, 2, { | ||
math: 'ceil' | ||
}) // 3.15 | ||
|
||
const floorResult = usePrecision(value, 3, { | ||
math: 'floor' | ||
}) // 3.141 | ||
``` |
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,30 @@ | ||
import { ref } from 'vue-demi' | ||
import { describe, expect, it } from 'vitest' | ||
import { usePrecision } from '.' | ||
|
||
describe('usePrecision', () => { | ||
it('should be defined', () => { | ||
expect(usePrecision).toBeDefined() | ||
}) | ||
it('should work', () => { | ||
const base = ref(45.125) | ||
const result = usePrecision(base, 2) | ||
expect(result.value).toBe(45.13) | ||
base.value = -45.155 | ||
expect(result.value).toBe(-45.15) | ||
}) | ||
it('out ceil should work', () => { | ||
const base = ref(45.125) | ||
const result = usePrecision(base, 2, { math: 'ceil' }) | ||
expect(result.value).toMatchInlineSnapshot('45.13') | ||
base.value = -45.151 | ||
expect(result.value).toMatchInlineSnapshot('-45.15') | ||
}) | ||
it('out floor should work', () => { | ||
const base = ref(45.129) | ||
const result = usePrecision(base, 2, { math: 'floor' }) | ||
expect(result.value).toMatchInlineSnapshot('45.12') | ||
base.value = -45.159 | ||
expect(result.value).toMatchInlineSnapshot('-45.16') | ||
}) | ||
}) |
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,31 @@ | ||
import type { ComputedRef } from 'vue-demi' | ||
import { computed } from 'vue-demi' | ||
import type { MaybeComputedRef } from '@vueuse/shared' | ||
import { resolveUnref } from '@vueuse/shared' | ||
|
||
export interface UsePrecisionOptions { | ||
/** | ||
* Method to use for rounding | ||
* | ||
* @default 'round' | ||
*/ | ||
math?: 'floor' | 'ceil' | 'round' | ||
} | ||
|
||
/** | ||
* Reactively set the precision of a number. | ||
* | ||
* @see https://vueuse.org/usePrecision | ||
*/ | ||
export function usePrecision( | ||
value: MaybeComputedRef<number>, | ||
digits: MaybeComputedRef<number>, | ||
options?: MaybeComputedRef<UsePrecisionOptions>, | ||
): ComputedRef<number | string> { | ||
return computed<number | string>(() => { | ||
const _value = resolveUnref(value) | ||
const _digits = resolveUnref(digits) | ||
const power = 10 ** _digits | ||
return Math[resolveUnref(options)?.math || 'round'](_value * power) / power | ||
}) | ||
} |