millify

repository·main·Indexed 41 days ago

https://github.com/izolate/millify

A utility library and CLI tool that converts large numbers into human-readable, abbreviated strings (e.g., 1000 to '1k'). Version 6.1.0 supports programmatic use via the millify() function with customizable MillifyOptions for precision, locales, and unit abbreviations.

Tokens
1K
Snippets
6
Records
7
Agent score
50%

What's inside millify

  1. Configure millify options

    main

    The millify function accepts an optional MillifyOptions object to customize the output format.

    NameTypeDefaultDescription
    precisionnumber1Number of decimal places to use
    localesstring | Array<string>browser languageFormats the number in different languages
    lowercasebooleanfalseUse lowercase abbreviations
    spacebooleanfalseAdd a space between number and abbreviation
    unitsArray<string>['', 'K', 'M', 'B', 'T', 'P', 'E']Unit abbreviations
    millify(39500, {
      precision: 2,  
      locales: "de-DE"
    });
    // 3,95K
    
    millify(1440000, {
      units: ["B", "KB", "MB", "GB", "TB"],
      space: true,
    });
    // 1.44 MB
  2. Use the millify() function programmatically

    main

    Import millify to convert numbers to formatted strings in your code.

    ESM Import: import millify from "millify";

    CommonJS Import: const { millify } = require("millify");

    import millify from "millify";
    
    millify(2500); // 2.5K
    
    millify(1024000, {
      precision: 3,
      lowercase: true
    });
    // 1.024m
  3. Configure Millify with MillifyOptions

    main

    When initializing Millify, you can provide a MillifyOptions object to customize how numbers are formatted.

    Key configuration properties include:

    • precision: The number of significant figures to display (default: 1).
    • locales: A BCP 47 language tag (e.g., `
  4. Use the millify function programmatically

    main

    The millify function is the primary entry point for the library. It is used to format large numbers into human-readable strings (e.g., converting 1000 to 1k). You can import it using either a named import or a default import depending on your module system configuration.

    import millify from 'millify';
    // or
    import { millify } from 'millify';
    
    const formatted = millify(1000);
    console.log(formatted); // '1k'
  5. Use the millify CLI to convert numbers

    main

    The millify command-line tool converts long numbers into human-readable strings (e.g., converting 1000 to 1k).

    Usage: millify <number> [options]

    Positional Arguments:

    • number: The numeric value you want to convert.

    Options:

    • --precision, -p (number, default: 1): The number of decimal places to include.
    • --decimal, -d (string, default: .): The character used as the decimal separator.
    • --lowercase, -l (boolean, default: false): If set, converts unit abbreviations to lowercase.
    • --space, -s (boolean, default: false): If set, adds a space between the number and the unit.
    • --units, -u (string): Specify custom unit abbreviations. You can provide multiple unit flags to define a sequence (e.g., -u B -u KB -u MB).
    # Run with default options
    millify 1000
    
    # Set precision to 2 decimal places
    millify 15025 --precision 2
    
    # Use lowercase units and add a space after the value
    millify 1000 --lowercase --space
    
    # Use a comma as the decimal separator
    millify 1000 --decimal=","
    
    # Specify custom units
    millify 1000 -u B -u KB -u MB -u GB