Socket
Socket
Sign inDemoInstall

numfmt

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

numfmt

Full Excel style number formatting


Version published
Weekly downloads
12K
increased by0.46%
Maintainers
1
Weekly downloads
 
Created
Source

numfmt – a spreadsheet number formatter

The numfmt library formats numbers according to a specifier string as defined in [ECMA-376][ecma]. The library tries its best to emulate the inns and outs of what the Excel spreadsheet software does.

The library is written in pure JavaScript and has no dependencies. It is comparable to the [SSF][ssf] with some minor interface exceptions.

Features

Why use this rather than the battle tested SSF? You may have no need to but numfmt is fully open source, has equivalent (if not better) formatting capabilities, built in international support, customizability, input parsing, and may run about twice as fast in most cases.

Adding locales is simple but those included are:

  • Chinese (Simplified) (zh-CN or zh)
  • Chinese (Traditional) (zh-TW)
  • Czech (cs)
  • Danish (da)
  • Dutch (nl)
  • English (en)
  • Finnish (fi)
  • French (fr)
  • German (de)
  • Greek (el)
  • Hungarian (hu)
  • Icelandic (is)
  • Indonesian (id)
  • Italian (it)
  • Japanese (ja)
  • Korean (ko)
  • Norwegian Bokmål (nb)
  • Polish (pl)
  • Portuguese (pt)
  • Russian (ru)
  • Slovak (sk)
  • Spanish (es)
  • Swedish (sv)
  • Thai (th)
  • Turkish (tr)

The library is made to work with current generation spreadsheets and so it does not support the 1904 date system. It does not have built in tables for formats addressable by offsets, you are expected to maintain those yourself.

Installing

If you don't want to download and build numfmt yourself, the library is conveniently provided as an NPM package:

$ npm install numfmt

Using

import { format } from "numfmt";

const output = format("#,##0.00", 1234.56);
console.log(output);

The full API documentation is available under API.md.

Format syntax

Microsoft have excellent documentation on how the format works. Here are some quick basics:

A format pattern is divided into 4 sections: <POSITIVE>;<NEGATIVE>;<ZERO>;<TEXT>

Only the first section is mandatory, the others are filled in as needed. The sections consist of symbols to indicate what to emit. The symbols are case insensitive:

SymbolResultDescription
0Digit or Zero7 formatted with 00 will emit "07"
#Digit if needed7 formatted with ## will emit "7"
?Digit or Space7 formatted with ?? will emit " 7"
.Decimal point
,Thousands separator1234 formatted with #,##0 will emit "1,234". The emitted grouping character depends on the locale used.
%PercentageNumber is multiplied by 100 before it is shown. .7 formatted with 0% will emit "70%"
E-, E+Exponential format12200000 formatted with 0.00E+00 will emit "1.22E+07"
$, -, +, /, (, ), Pass-throughThe symbol is printed as-is.
\EscapePass the the next character through as-is.
*FillRepeat the next character in the format enough times to fill the column to its current width. Like Excel's TEXT function, numFmt emits nothing when this is used.
_Skip widthSkip the width of the next character. Like Excel's TEXT function, numFmt emits only a single space when this is used.
"text"Pass-throughPass through whatever text is inside the quotation marks as-is. 7 formatted with 0 "bells" will emit "7 bells"
@Text valueWhen value is a text, emit it as is: foo formatted with "bar"@"bar" will emit "barfoobar"
yyYearsTwo digit year
yyyyYearsFour digit year
mMonth1–12
mmMonth01–12
mmmShort monthMonth name abbreviation (Jan–Dec). Names are locale dependent.
mmmmMonth nameFull month name (January–December). Names are locale dependent.
mmmmmMonth nameSingle letter month abbreviation (J–D). Names are locale dependent.
dDays1–31
ddDays01–31
dddWeekdaysSun–Sat
ddddWeekdaysSunday–Saturday
hHours0–23 or 1–12
hhHours00–23 or 01–12
mMinutes0–59
mmMinutes00–59
sSeconds0–59
ssSeconds00–59
AM/PM12h clockSets clock to 12h and emits AM or PM.
A/P12h clockSets clock to 12h and emits A or P.
[h]HoursElapsed time in hours
[m]MinutesElapsed time in minutes
[s]SecondsElapsed time in seconds

The <POSITIVE> and <NEGATIVE> sections may optionally have conditionals. A condition is set by a comparison operator followed by a number, followed by the regular format symbols, surrounded by square brackets: [>=-2.5]#,##0.0. The supported set of operators is: = > < >= <= <>

  • There must not be more than 4 sections.
  • There must not be more than 2 conditional sections.
  • There must not be numerical or date emitting symbol in the (4th) text part.

Keywords

FAQs

Package last updated on 16 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc