format
printf, sprintf, and vsprintf for JavaScript
Last updated 20 days ago by sjs .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install format 
SYNC missed versions from official npm registry.

format

printf, sprintf, and vsprintf for JavaScript

version on npm node version 12 and up MIT License

Installation

npm install format

In a browser without a bundler, load the minified build with a script tag. It defines format, printf, and vsprintf as globals.

<script src="https://cdn.jsdelivr.net/npm/format@1/builds/format.min.js"></script>

Usage

import format from 'format';
// or with CommonJS
// const format = require('format');
const { printf, vsprintf } = format;
// or if you want to keep it old school
const sprintf = format;

// Print 'hello world'
printf('%s world', 'hello');

const what = 'life, the universe, and everything';
const out = format('%d is the answer to %s', 42, what);
console.log(out);
// => '42 is the answer to life, the universe, and everything'

vsprintf('%d is the answer to %s', [42, what]);
// => '42 is the answer to life, the universe, and everything'

// you can format values as JSON with %j
const value = { answer: 42 };
const out2 = format('%j', value);
console.log(out2);
// => '{"answer":42}'

Supported format specifiers: b, c, d, f, j, o, s, x, and X.

See man 3 printf or man 1 printf for details. j is an extension that formats values as JSON.

Precision is supported for floating point numbers.

License

Copyright 2010 - 2026 Sami Samhuri sami@samhuri.net

MIT license

Current Tags

  • 1.0.1                                ...           latest (20 days ago)

10 Versions

  • 1.0.1                                ...           20 days ago
  • 1.0.0                                ...           20 days ago
  • 0.2.2                                ...           11 years ago
  • 0.2.1                                ...           13 years ago
  • 0.2.0                                ...           14 years ago
  • 0.1.4                                ...           15 years ago
  • 0.1.3                                ...           15 years ago
  • 0.1.2                                ...           15 years ago
  • 0.1.1                                ...           15 years ago
  • 0.1.0                                ...           15 years ago
Maintainers (1)
Downloads
Total 43
Today 0
This Week 0
This Month 15
Last Day 0
Last Week 13
Last Month 11
Dependencies (0)
None
Dev Dependencies (2)
Dependents (2)

© 2010 - cnpmjs.org x YWFE | Home | YWFE