Skip to content

v-number

Format and validate numeric input with customizable options.

Since: 1.2.0

Usage

Basic

vue
<template>
  <input v-number v-model="quantity" />
</template>

With Constraints

vue
<template>
  <input v-number="{ min: 0, max: 100, step: 5 }" v-model="count" />
</template>

API

Types

typescript
interface NumberOptions {
  /** Minimum value */
  min?: number
  /** Maximum value */
  max?: number
  /** Step increment */
  step?: number
  /** Decimal places @default 0 */
  decimals?: number
  /** Allow negative numbers @default true */
  negative?: boolean
  /** Thousands separator @default ',' */
  thousands?: string
  /** Decimal separator @default '.' */
  decimal?: string
  /** Disable formatting @default false */
  disabled?: boolean
}

type NumberBinding = boolean | NumberOptions

Options

OptionTypeDefaultDescription
minnumber-Minimum allowed value
maxnumber-Maximum allowed value
stepnumber1Step increment
decimalsnumber0Decimal places
negativebooleantrueAllow negative numbers
thousandsstring','Thousands separator
decimalstring'.'Decimal separator
disabledbooleanfalseDisable formatting

Examples

Percentage Input

vue
<template>
  <input v-number="{ min: 0, max: 100 }" v-model="percentage" />
</template>

Quantity Selector

vue
<template>
  <input v-number="{ min: 1, max: 99, step: 1 }" v-model="quantity" />
</template>

Decimal Input

vue
<template>
  <input v-number="{ decimals: 2, thousands: '' }" v-model="rate" />
</template>

Positive Only

vue
<template>
  <input v-number="{ negative: false, min: 0 }" v-model="amount" />
</template>

Composable API

For programmatic use, you can use the useNumber composable:

typescript
import { useNumber } from 'directix'

const { formatted, value, parse } = useNumber({
  value: 1234567,
  precision: 2,
  separator: ',',
  decimal: '.',
  prefix: '$',
  suffix: ' items'
})
// formatted.value = '$1,234,567.00 items'

UseNumberOptions

OptionTypeDefaultDescription
valuenumber | Ref<number>-The numeric value (required)
precisionnumber | Ref<number>0Decimal places
separatorstring | Ref<string>','Thousands separator
decimalstring | Ref<string>'.'Decimal separator
prefixstring | Ref<string>''Prefix string
suffixstring | Ref<string>''Suffix string

UseNumberReturn

PropertyTypeDescription
formattedRef<string>The formatted number string
valueRef<number>The numeric value
parse(formatted: string) => numberParse formatted string to number

Example

vue
<script setup>
import { ref } from 'vue'
import { useNumber } from 'directix'

const count = ref(1234567)

const { formatted } = useNumber({
  value: count,
  precision: 2,
  suffix: ' items'
})
</script>

<template>
  <span>Total: {{ formatted }}</span>
  <!-- Output: Total: 1,234,567.00 items -->
</template>

Utility Functions

typescript
import { formatNumber, parseNumber, createNumberFormatter } from 'directix'

// Format directly
formatNumber(1234567) // '1,234,567'
formatNumber(1234.56, { precision: 2, suffix: '%' }) // '1,234.56%'

// Parse formatted string
parseNumber('1,234,567') // 1234567

// Create reusable formatter
const formatPercent = createNumberFormatter({ suffix: '%', precision: 1 })
formatPercent(85.5) // '85.5%'

Code Generator

Quick Code Generator
<template>
  <div v-number="{ decimals: 0, separator: ',' }">
    <!-- Format and validate numeric input with customizable options. directive -->
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'

// Configure your options here
const options = { decimals: 0, separator: ',' }
</script>

Released under the MIT License.