Skip to main content

API Reference

Complete API documentation for all vn-number functions with detailed examples and type signatures
3 min read

Type Definitions#

NumberType#

The input type accepted by all formatting functions.

All functions that format numbers accept this union type, providing flexibility in handling various input sources.


Reading Functions#

readVnNumber()#

Converts a number to Vietnamese text representation.

Parameters:

  • number - The number to read. Can be a string, number, or bigint value

Returns: Vietnamese text representation of the number

Examples:

Vietnamese Language Rules:

The function follows these Vietnamese language conventions:

  1. "lăm" rule: The digit 5 is read as "lăm" instead of "năm" when in the ones position of numbers 15, 25, 35, etc.

  2. "mốt" rule: The digit 1 is read as "mốt" instead of "một" when in the ones position after tens (21, 31, 41, etc.)

  3. "lẻ" rule: Used when there's a single digit after hundreds (101, 102, etc.)

  4. Zero handling: Zeros in middle groups are read as "không trăm"

Use Cases:

  • Bank checks and financial documents
  • Invoice text generation
  • Accessibility features (reading prices aloud)
  • Educational applications
  • Voice assistants

Formatting Functions#

formatVnNumber()#

Formats a number using Vietnamese number format (dots as thousand separators).

Parameters:

  • number - The number to format (string, number, bigint, null, or undefined)
  • fallbackValue - The value to return if formatting fails (default: '0')

Returns: Formatted number with dots as thousand separators, or the fallback value

Examples:

Use Cases:

  • Displaying numbers in Vietnamese format
  • Data tables and reports
  • Dashboard statistics
  • User profile information (followers, likes, etc.)

formatVnCurrency()#

Formats a value as Vietnamese Dong (VND) currency.

Parameters:

  • money - The monetary value to format (string, number, bigint, null, or undefined)
  • fallbackValue - The value to return if formatting fails (default: '0 ₫')

Returns: Formatted currency with VND symbol (₫), or the fallback value

Examples:

Use Cases:

  • E-commerce price displays
  • Payment forms and receipts
  • Financial dashboards
  • Shopping cart totals
  • Pricing tables
  • Accounting applications

formatVnPercent()#

Formats a number or decimal as a Vietnamese percentage.

Parameters:

  • value - The value to format (0-1 for percentages, or any number)
  • fallbackValue - The value to return if formatting fails (default: '0%')

Returns: Formatted percentage with % symbol, or the fallback value

Note: The function multiplies the input by 100, so 0.5 becomes 50%. Use 5 directly if you want 500%.

Examples:

Use Cases:

  • Growth rate displays
  • Discount badges in e-commerce
  • Statistical dashboards
  • Progress indicators
  • Financial metrics (ROI, profit margin, etc.)
  • Survey results

Advanced Examples#

Complete E-commerce Product Card#

Financial Dashboard Statistics#

Bank Check Generator#

Form Input Helpers#

Error Handling#

All formatting functions (formatVnNumber, formatVnCurrency, formatVnPercent) gracefully handle invalid input:

Note

Type Safety

The readVnNumber function expects valid numeric input (string, number, or bigint). Always validate user input before passing to this function. The formatting functions are more forgiving and provide fallback values for invalid input.

Browser and Runtime Compatibility#

vn-number works in all modern JavaScript environments:

  • ✅ Node.js 14+
  • ✅ Bun
  • ✅ Deno
  • ✅ Modern browsers (Chrome, Firefox, Safari, Edge)
  • ✅ React Native
  • ✅ Electron
  • ✅ Edge Runtime (Vercel, Cloudflare Workers, etc.)