Skip to content

usePrecision

Category
Export Size
199 B
Package
@reause/math
Last Changed
5 days ago

Reactively set the precision of a number

Demo

Usage

tsx
import { 
usePrecision
} from '@reause/math'
const
result
=
usePrecision
(3.1415, 2) // 3.14
const
ceilResult
=
usePrecision
(3.1415, 2, {
math
: 'ceil'
}) // 3.15 const
floorResult
=
usePrecision
(3.1415, 3, {
math
: 'floor'
}) // 3.141

value, digits and options are plain read-only values (upstream takes MaybeRefOrGetter<...>). Re-render with new values — e.g. from useState — and the hook recomputes.

Type Declarations

ts
export interface UsePrecisionOptions {
    math?: 'floor' | 'ceil' | 'round';
}

export function usePrecision(value: number, digits: number, options?: UsePrecisionOptions): number

Source

Source · Demo · VueUse

Contributors

hairyf

Changelog

v0.1.0 on
2fae8 - fix(math): resolve usePrecision audit findings (#790)
8fddc - chore!: remove all Vue-only Maybe* types and getter unions, adopt React Ref (#462)

Released under the MIT License. v0.1.8