DEV Community

Indian Modassir
Indian Modassir

Posted on • Edited on

JavaScript sprintf implementation

🌟 Features

  • Fully functional printf, sprintf, and vsprintf methods.
  • C-style format specifiers support:
    • %d, %f, %s, %x, %b, %o, %u, %c, %e, %g, %G, etc.
  • Padding, alignment, width, and precision controls
  • Argument indexing like %2$08.2f
  • Supports most common C-style format specifiers.
  • Distinct handling for %f (locale) and %F (non-locale)
  • Float, Integer, Hex, Octal, Binary, Char, and String formatting
  • Custom width, padding, alignment flags.
  • Float precision control up to JavaScript's max (53 bits).
  • Handles NaN, Infinity and signed values.
  • Works in Node.js, modern browsers, and CommonJS/AMD environments.
  • No external dependencies

How to install printfy

To include printfy in Node, first install with npm.

npm install printfy
Enter fullscreen mode Exit fullscreen mode

How to build printfy

Clone a copy of the main Sizzle git repo by running:

git clone git://github.com/jsvibe/printfy.git
Enter fullscreen mode Exit fullscreen mode

In the printfy/dist folder you will find build version of printfy along with the minified file.

Including printfy

Below are some of the most common ways to include printfy.

Browser

CDN Link

<script src="https://cdn.jsdelivr.net/npm/printfy@2.0.2/dist/printfy.min.js"></script>
Enter fullscreen mode Exit fullscreen mode

You can add the script manually to your project:

<script src="printfy.js"></script>
Enter fullscreen mode Exit fullscreen mode

Webpack / Browserify / Babel

There are several ways to use Webpack, Browserify or Babel. For more information on using these tools, please refer to the corresponding project's documentation. In the script, including printfy will usually look like this:

import {sprintf, printf, vprintf, vsprintf} from "printfy";
Enter fullscreen mode Exit fullscreen mode

Node.js

Fully compatible with Node.js, this library lets you use familiar C-style printf formatting in server-side code—ideal for CLI tools, logging, and backend output formatting.

const {sprintf, printf, vprintf, vsprintf} = require("printfy");
Enter fullscreen mode Exit fullscreen mode

API Usage

printf(format, ...args)

Logs the formatted output to the console.

printf("Hello %s, you have %d new messages", "Alice", 5);
// Output: Hello Alice, you have 5 new messages
Enter fullscreen mode Exit fullscreen mode

vprintf(format, args[])

Logs the formatted output to the console.

printf("Hello %s, you have %d new messages", [ "Alice", 5]);
// Output: Hello Alice, you have 5 new messages
Enter fullscreen mode Exit fullscreen mode

sprintf(format, ...args)

Returns a formatted string (like sprintf in C).

let result = sprintf("Value: %08.2f", 3.14);
console.log(result); // Output: Value: 00003.14
Enter fullscreen mode Exit fullscreen mode

vsprintf(format, args[])

Same as sprintf, but accepts arguments as an array.

vsprintf("User: %s, Score: %d", ["Bob", 100]);
// Output: User: Bob, Score: 100
Enter fullscreen mode Exit fullscreen mode

🔢 Supported Specifiers

Specifier Meaning Example
%s String sprintf("%s", "abc")"abc"
%S Uppercase string "abc""ABC"
%d Integer (decimal) 42
%u Unsigned integer (64-bit) -1"18446744073709551615"
%b Binary 5"101"
%o Octal 8"10"
%x, %X Hexadecimal (lower/upper) 255"ff" or "FF"
%f, %F Float (fixed-point) 3.14"3.140000"
%e, %E Float (scientific notation) 3.14"3.14e+0"
%g, %G Auto float/scientific (shortest form) 123000"1.23e+5"
%c Character from ASCII code 65"A"
%% Literal percent sign %

⚙️ Format Options

Format syntax:

%[index$][padding][flag][width][.precision]specifier
Enter fullscreen mode Exit fullscreen mode

🧪 Examples

sprintf("Binary: %08b", 5);          // Binary: 00000101
sprintf("Hex: %#x", 255);            // Hex: ff
sprintf("Char: %c", 65);             // Char: A
sprintf("Padded: %10s", "text");     // Padded:      text
sprintf("Left: %-10s!", "text");     // Left: text     !
sprintf("Float: %.2f", 3.14159);     // Float: 3.14
sprintf("Scientific: %.2e", 1200);   // Scientific: 1.20e+3
Enter fullscreen mode Exit fullscreen mode

[index$] – Argument Indexing

sprintf('%2$s is %1$d years old.', 22, 'Modassir');
// Output: Modassir is 22 years old.
Enter fullscreen mode Exit fullscreen mode

[padding] – Custom Padding Character

sprintf("%'~5d", 42);
// Output: ~~~42

sprintf("%'~-5d", 42);
// Output: 42~~~
Enter fullscreen mode Exit fullscreen mode

[flag] – Format Flags

Support flags + and - only.

sprintf("%+d", 42);
// Output: +45

sprintf("%-d", 42);
// Output: -45
Enter fullscreen mode Exit fullscreen mode

[width] – Custom Width

sprintf("%6s", "JS");
// Output: "    JS"

sprintf("lang%6s", "JS");
// Output: "lang    JS"

sprintf("%-6s", "JS");
// Output: "JS    "

sprintf("%-6slib", "JS");
// Output: "JS    lib"
Enter fullscreen mode Exit fullscreen mode

[.precision] – Precision (floating-point or string truncation)

sprintf("%.2f", 3.14159);
// Output: 3.14

sprintf("%.4s", "OpenAI");
// Output: Open
Enter fullscreen mode Exit fullscreen mode

Full Format

sprintf('%2$\'#-+10.2f and %1$\'*_10s', "JS", 3.14159);
// Output: +3.14#####JS*******
Enter fullscreen mode Exit fullscreen mode

🔹 Explanation:

  • %2$: Second argument (3.14159)
  • '#: padding character #
  • -: left align
  • +: show sign
  • 10: total width 10
  • .2f: 2 decimal places
  • %1$'*_10s: First argument (JS), padded with * to width 10

⚠️ Errors & Warnings

  • ArgumentCountError: Thrown if insufficient arguments are passed for format specifiers.
  • ValueError: Invalid specifier detected.
  • RangeError: Padding exceeds safe integer limit.
  • Precision Truncation: Float precision is capped at JavaScript's safe limit (53 digits).

🔄 Comparison with Other Libraries

Feature / Library printfy ✓ sprintf-js 🟡 fast-printf 🟢 printf (npm) 🔵
C-style specifiers ✓ Full ✓ Full ✓ Partial ✓ Partial
%2\$ style arg index ✓ Yes ✓ Yes ✗ No ✗ No
BigInt support ✓ Yes ✗ No ✗ No ✗ No
%f vs %F (locale) ✓ Separate ✗ Combined ✗ Combined ✗ No
Custom padding ('x) 'x, 0, etc. ✗ No ✗ No ✗ No
String return (sprintf) ✓ Yes ✓ Yes ✓ Yes ✗ No
Lightweight / No deps ✓ Yes ✓ Yes ✓ Yes ✓ Yes
Unicode emoji safe Partial Partial ✗ No ✗ No
Performance Good Good Best Good

📦 Module Support

This library supports the following environments:

  • Browser (<script>)
  • CommonJS / Node.js
  • AMD (Asynchronous Module Definition)

🔐 License

MIT License © 2025 Indian Modassir

See LICENSE for details.


🙌 Contributions

Pull requests, bug reports, and feedback are welcome!

Visit the GitHub Repository to contribute.

Top comments (5)

Collapse
 
coding_modassir_be62751cb profile image
Coding Modassir

Superb bro

Collapse
 
saniya_parween_df4ecbca20 profile image
Saniya Parween

Great hard work better and smooth performance

Collapse
 
s_s_310adacfa profile image
sʜᴀʜᴢᴀᴅɪ ᴀғsᴀʀᴀ

excellent

Collapse
 
indianmodassir profile image
Indian Modassir

Ask me any query

Some comments may only be visible to logged-in visitors. Sign in to view all comments.