Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. sprintf() / number_format()

sprintf() / number_format() Since: PHP 4(2000)

Formats strings and numbers using a format string. For digit grouping and decimal precision, number_format() is a convenient option.

Syntax

// Formats a string according to the format specifier.
sprintf($format, ...$values);

// A version that accepts arguments as an array.
vsprintf($format, $values);

// Formats a number with digit grouping.
number_format($number, $decimals, $decimal_separator, $thousands_separator);

Function List

FunctionDescription
sprintf($format, ...$values)Returns a string formatted according to the format specifier.
vsprintf($format, $values)The array-argument version of sprintf(). Use this when passing arguments dynamically.
number_format($number, $decimals, $dec, $sep)Returns a number formatted as a string with digit grouping. $decimals sets the number of decimal places, $dec sets the decimal symbol, and $sep sets the thousands separator.

Return Value

Returns the formatted string.

Sample Code

<?php
// %s is replaced with a string, %d with an integer.
echo sprintf("Name: %s, Age: %d", "Taro Yamada", 30); // Outputs: 'Name: Taro Yamada, Age: 30'

// %05d formats the number as a zero-padded 5-digit integer.
echo sprintf("Order number: %05d", 42); // Outputs: 'Order number: 00042'

// %0.2f formats the number with 2 decimal places.
echo sprintf("Amount: %0.2f", 1234.5); // Outputs: 'Amount: 1234.50'

// %% outputs a literal percent sign.
echo sprintf("Achievement: %d%%", 85); // Outputs: 'Achievement: 85%'

// You can specify the order of arguments.
echo sprintf("%2\$s at %1\$s.", "Tokyo branch", "Yamada"); // Outputs: 'Yamada at Tokyo branch.'

// vsprintf() accepts arguments as an array.
$params = ["Product A", 3, 1500];
echo vsprintf("Purchased %s x%d, total $%d.", $params); // Outputs: 'Purchased Product A x3, total $1500.'

// number_format() adds digit grouping separators.
echo number_format(1234567); // Outputs: '1,234,567'

// Display with 2 decimal places.
echo number_format(1234567.891, 2); // Outputs: '1,234,567.89'

// Example with customized separator symbols.
echo number_format(1234567.89, 2, ".", ","); // Outputs: '1,234,567.89'

// Example of formatting a price value.
$price = 198000;
echo "$" . number_format($price); // Outputs: '$198,000'

Overview

sprintf() is a format function originating from C, and it lets you format strings and numbers into a specified layout. The main conversion specifiers are: %s for strings, %d for integers, %f for floating-point numbers, and %x for hexadecimal. Zero-padding and width specifiers are also supported, making it useful for generating order numbers or sequential filenames.

number_format() is a dedicated function for adding digit grouping to numbers. It is convenient for displaying prices and formatting data for readability, and you can customize the number of decimal places and separator symbols. The return value of number_format() is a string, so if you need to perform calculations with it, you must convert it back to a number first.

If you want to output directly to the screen, you can use printf() instead of echo sprintf() for the same result. For joining strings, implode() is useful, and for padding strings, str_pad() is a good option.

If you find any errors or copyright issues, please .