Have ideas to improve npm?Join in the discussion! »

    @ionaru/format-number
    TypeScript icon, indicating that this package has built-in type declarations

    3.0.1 • Public • Published

    @ionaru/format-number

    npm version npm version Build Status codecov

    Description

    A package for formatting a number to improve its readability.

    Usage

    npm install @ionaru/format-number

    Standard use

    import { formatNumber } from 'format-number';
    const formattedNumber = formatNumber(500.8);
    console.log(formattedNumber);  // 500.80

    No decimals

    import { formatNumber } from 'format-number';
    const formattedNumber = formatNumber(1.62, 0);
    console.log(formattedNumber);  // 2

    API

    formatNumber(amount, decimalAmount, decimalMark, delimiter) ⇒ string

    Format a number to make it more readable.

    Kind: global function

    Param Type Default Description
    amount number | string The amount that will be formatted, must be convertible by Number().
    decimalAmount number 2 The amount of decimal places will be in the formatted number.
    decimalMark string "." The character that marks the start the fractional part of the formatted number.
    delimiter string "," The character that separates the thousands in a number.

    Examples

    Standard formatting is great for monetary values.

    const formattedNumber = formatNumber(19499.99);
    console.log(formattedNumber);  // 19,499.99

    Show only whole numbers

    const formattedNumber = formatNumber(19499.99, 0);
    console.log(formattedNumber);  // 19,500

    The input number can be a string

    const formattedNumber = formatNumber('19499.99');
    console.log(formattedNumber);  // 19,499.99

    Change the default markings in the number for international use.

    Canadian

    const formattedNumber = formatNumber(19499.99, undefined, ',', ' ');
    console.log(formattedNumber);  // 19 499,99

    Dutch

    const formattedNumber = formatNumber(19499.99, undefined, ',', '.');
    console.log(formattedNumber);  // 19.499,99

    Keywords

    none

    Install

    npm i @ionaru/format-number

    DownloadsWeekly Downloads

    18

    Version

    3.0.1

    License

    MIT

    Unpacked Size

    11.3 kB

    Total Files

    7

    Last publish

    Collaborators

    • avatar