Skip to content

⬅️ Back to Table of Contents

📄 usePrecision

📊 Analysis Summary

Metric Count
🔧 Functions 2
📦 Imports 4
🟢 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

Vue Composition API

Name Type Reactive Variables Composables
computed computed none none

Functions

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

Reactively set the precision of a number.

See: https://vueuse.org/usePrecision

Tags: @__NO_SIDE_EFFECTS__

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

Calls:

  • computed (from vue)
  • toValue (from vue)
  • complex_call_1212
  • accurateMultiply
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
  })
}

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

Accuracy of handling numerical values.

Parameters:

  • value any: The value
  • power any: The power

Returns: undefined The result of multiplying the value with the power

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

Calls:

  • value.toString
  • valueStr.includes
  • valueStr.split
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
  }
}

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' not shown

Generated by Syntax Scribe