R
fn

useToNumber

v0.0.15testeddemo

Reactively convert a string or number ref to a number.

Examples

ts
const str = ref('42.5');
const num = useToNumber(str); // 42.5
ts
// custom converter and clamping
const n = useToNumber(input, { method: v => Math.round(+v), min: 0, max: 100 });

Demo

Loading demo…

Signature

ts
export function useToNumber(
  value: MaybeRefOrGetter<number | string>,
  options: UseToNumberOptions ={ ... }

Parameters

ParameterTypeDescription
valueMaybeRefOrGetter<string | number>The source value (can be reactive)
options?UseToNumberOptionsOptions

Returns

ComputedRef<number>The numeric value