/
index.ts
28 lines (26 loc) · 936 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import type { ComputedRef } from 'vue-demi'
import { computed } from 'vue-demi'
import type { MaybeComputedRef } from '@vueuse/shared'
import { resolveUnref } from '@vueuse/shared'
export interface FixedTypes {
type?: 'string' | 'number'
math?: 'floor' | 'ceil' | 'round'
}
/**
* @deprecated use `usePrecision` instead
*/
export function useToFixed(
value: MaybeComputedRef<number | string>,
digits: MaybeComputedRef<number>,
options?: MaybeComputedRef<FixedTypes>,
): ComputedRef<number | string> {
return computed<number | string>(() => {
const floatValue = parseFloat(`${resolveUnref(value)}`)
const outValue = Math[resolveUnref(options)?.math || 'round'](floatValue * 10 ** resolveUnref(digits)) / 10 ** resolveUnref(digits)
return resolveUnref(options)?.type === 'string'
? resolveUnref(digits) >= 0
? outValue.toFixed(resolveUnref(digits))
: `${outValue}`
: outValue
})
}