Skip to content

⬅️ Back to Table of Contents

📄 index.ts

📊 Analysis Summary

Metric Count
🔧 Functions 2
📦 Imports 4
📊 Variables & Constants 3
🟢 Vue Composition API 1
📐 Interfaces 1

📚 Table of Contents

🛠️ File Location:

📂 packages/math/usePrecision/index.ts

📦 Imports

Name Source
ComputedRef vue
MaybeRefOrGetter vue
computed vue
toValue vue

Variables & Constants

Name Type Kind Value Exported
decimalPlaces number const valueStr.split('.')[1].length
multiplier number const 10 ** decimalPlaces
power number const 10 ** _digits

Vue Composition API

Name Type Reactive Variables Composables
computed computed none none

Functions

accurateMultiply(value: number, power: number): number

Code
function accurateMultiply(value: number, power: number): number {
  const valueStr = value.toString()

  if (value > 0 && valueStr.includes('.')) {
    const decimalPlaces = valueStr.split('.')[1].length
    const multiplier = 10 ** decimalPlaces

    return (value * multiplier * power) / multiplier
  }
  else {
    return value * power
  }
}
  • JSDoc:

    /**
     * Accuracy of handling numerical values.
     *
     * @param value - The value
     * @param power - The power
     * @returns The result of multiplying the value with the power
     */
    

  • Parameters:

  • value: number
  • power: number
  • Return Type: number
  • Calls:
  • value.toString
  • valueStr.includes
  • valueStr.split

usePrecision(value: MaybeRefOrGetter<number>, digits: MaybeRefOrGetter<number>, options: MaybeRefOrGetter<UsePrecisionOptions>): ComputedRef<number>

Code
export function usePrecision(
  value: MaybeRefOrGetter<number>,
  digits: MaybeRefOrGetter<number>,
  options?: MaybeRefOrGetter<UsePrecisionOptions>,
): ComputedRef<number> {
  return computed<number>(() => {
    const _value = toValue(value)
    const _digits = toValue(digits)
    const power = 10 ** _digits
    return Math[toValue(options)?.math || 'round'](accurateMultiply(_value, power)) / power
  })
}
  • JSDoc:

    /**
     * Reactively set the precision of a number.
     *
     * @see https://vueuse.org/usePrecision
     */
    

  • Parameters:

  • value: MaybeRefOrGetter<number>
  • digits: MaybeRefOrGetter<number>
  • options: MaybeRefOrGetter<UsePrecisionOptions>
  • Return Type: ComputedRef<number>
  • Calls:
  • computed (from vue)
  • toValue (from vue)
  • complex_call_1185
  • accurateMultiply

Interfaces

UsePrecisionOptions

Interface Code
export interface UsePrecisionOptions {
  /**
   * Method to use for rounding
   *
   * @default 'round'
   */
  math?: 'floor' | 'ceil' | 'round'
}

Properties

Name Type Optional Description
math 'floor' | 'ceil' | 'round'