Appearance
usePrecision
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.141value, 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