Skip to content
samsonjsPublic

About

printf-like formatting for JavaScript

Resources

Stars

52 stars

Watchers

1 watching

Forks

Repository files navigation

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

About

printf-like formatting for JavaScript

Resources

Stars

52 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages