Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including si-prefix with all npm packages installed. Try it out:

var siPrefix = require("si-prefix")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

si-prefix v0.2.0

SI and IEC compatible numeric formatter

SI Prefix

Module for converting SI base units into human readable form. It can also be easily extended to create other units.

Build Status

Install

$ npm install si-prefix

Examples

Basic Formatting

var si = require('si-prefix');

var millimetres = si.metre.format(0.035, ' '),          //=> '35 mm'
    nanograms = si.kilogram.suffix(0.00000000094, ' '), //=> 'ng'
    kibibytes = si.byte.convert(2048),                  //=> [ 2, 'KiB' ]
    kilobytes = si.byte.decimal.convert(2048);          //=> [ 2.048, 'kB' ]

Units available by default are:

  • the seven SI base units
    • metre
    • kilogram - note that the base unit for mass is a kilogram, NOT a gram
    • second
    • ampere
    • kelvin
    • mole
    • candela
  • some SI derived units
    • litre
  • a few others
    • byte - using IEC binary prefixes
    • byte.decimal - using SI prefixes
    • meter - alias of metre
    • liter - alias of litre

Custom Units

var si = require('si-prefix');

// scale with hecto and deca prefixes
var scale = new si.Scale({minor: true}),
    unit = new si.Unit(scale, 'm');

var centimetres = unit.format(0.03, ' '); //=> '3 cm'

Other ways you can customise units include changing the prefixes, removing some exponents, altering the logarithm base and shifting prefixes up or down. You can see examples of this in scale.coffee where Scale.Binary and Scale.Mass are defined.

Upcoming

  • Separate flags for hecto, deca, deci and centi
  • Option to switch between mc and µ for micro prefix
  • Other stuff? File issues if you got 'em!

FAQ

Why is it called si-prefix when it deals with suffixes?

<_<;;

_>;;

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free