C-style printf/sprintf for JavaScript

0.2.2 · abandoned · verified Sun Apr 19

The `format` package (current version 0.2.2) provides `printf`, `sprintf`, and `vsprintf` functions for JavaScript, emulating the string formatting capabilities found in the C standard library. It supports a range of format specifiers including `b`, `c`, `d`, `f`, `o`, `s`, `x`, and `X`, along with precision control for floating-point numbers. The package was last updated in 2014 and explicitly targets Node.js versions 0.4.x and above, as well as web browsers by direct script inclusion. Its core value proposition was to offer a familiar C-style formatting API in JavaScript environments. Due to its age and lack of maintenance over the last decade, it should be considered an abandoned project, potentially incompatible with modern JavaScript ecosystems and lacking critical updates for security or compatibility.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core `sprintf`, `printf`, and `vsprintf` functions with various format specifiers and precision settings.

const format = require('format');
const printf = format.printf;
const vsprintf = format.vsprintf;

// Using sprintf (which is the default export of the 'format' module)
const result1 = format('%d is the answer to %s', 42, 'life, the universe, and everything');
console.log('sprintf example:', result1);

// Using printf to log directly to console (adds a newline for convenience)
console.log('\nprintf example:');
printf('%s world\n', 'hello');

// Using vsprintf with an array of arguments
const what = 'life, the universe, and everything';
const result2 = vsprintf('%d is the answer to %s', [42, what]);
console.log('\nvsprintf example:', result2);

// Demonstrating different format specifiers and precision
console.log(format('Binary: %b, Char: %c, Decimal: %d, Float: %.2f, Octal: %o, String: %s, Hex (lower): %x, Hex (upper): %X', 
  10, 'A', 123, 3.14159, 10, 'test string', 255, 255));

view raw JSON →