Feb 262015
Article PHP

PHP uses a default format when a number is sent to standard output or to a file, but this format might not be adequate in some cases.

This post explains some of the options available in PHP to apply different representations to integer and floating point numbers.

To illustrate this post, we will use a sample script that includes different cases that may occur:

$num1 = 1234;
$num2 = 12345678901234567890;
$num3 = 1234.0;
$num4 = 123456789012345.0;
$num5 = 0.1234;
$num6 = 0.0000001234;
$num7 = 1234.56789;

echo "A: " . $num1 . "\n";
echo "B: " . $num2 . "\n";
echo "C: " . $num3 . "\n";
echo "D: " . $num4 . "\n";
echo "E: " . $num5 . "\n";
echo "F: " . $num6 . "\n";
echo "G: " . $num7 . "\n";

When this snippet is run, the following result is printed:

A: 1234
B: 1.2345678901235E+19
C: 1234
D: 1.2345678901234E+14
E: 0.1234
F: 1.234E-7
G: 1234.56789

In this output, we see that:

  1. A small integer number is printed with no special formatting
  2. A very large integer number is printed in scientific notation.
  3. A floating point number is printed as an integer, if it has no decimal part.
  4. A very large floating point number is printed in scientific notation.
  5. If the number of decimal digits is small, a floating point number is printed using the dot character as separator
  6. If the floating point number is much smaller that the unit, it is printed in scientific notation.
  7. In other cases, a floating point number is printed using the dot character as separator.

Specifying a numeric format with printf()

The first argument of the built-in function printf() is a format specifier string. This specifier is used to format the following arguments. The syntax of printf() is:

printf ( string $format [, mixed $args [, mixed $... ]] )

There is also a sprintf() function with the same syntax. sprintf returns the resulting text string, instead of sending it to standard output.

The firsts argument to printf contains a series of one or more format specifiers identified by the % sign followed by a character. For instance, the hexadecimal representation of an integer number can be obtained with the sentence:

$hexadecimal = sprintf("%x",$number);

 Format specifiers for integers

  • %x, %X – Generate the hexadecimal representation of the number, in lowercase/uppercase
  • %b – Generate the binary representation of an integer
  • %o – Generate the octal representation of an integer
  • %d, %u – Generate the signed/unsigned decimal representation of an integer

Example 1:

The following statements:

$num1 = 1234;
printf ("Decimal: %d, Binary: %b, Octal: %o\n",
         $num1, $num1, $num1);
printf ("Hex 1: %x, Hex 2: %4x, Hex 3: %04x\n",
         $num1, $num1, $num1);

produce the output:

Decimal: 1234, Binary: 10011010010, Octal: 2322
Hex 1: 4d2, Hex 2:  4d2, Hex 3: 04d2

In this example, we can see the effect of using precision modifiers in the specifiers: “%04x” fills the result with leading zeroes up to four digits, while  “%4x” fills with leading spaces.

Example 2:

The statements:

$num1 = -1234;
printf("Unsigned decimal: %u, Hexadecimal: %X\n",$num1,$num1);

Print to screen:

Unsigned decimal: 18446744073709550382, Hexadecimal: FFFFFFFFFFFFFB2E

Due to the fact that we are running the example on a 64 bit system, the unsigned decimal representation of a negative number N is  2^64 + N, and the hexadecimal representation is 16 digits long each digit 0..F representing four bits.

Format specifiers for floating point numbers

  •  %f, %F – Generate the normal representation of floatint point numbers as <integer-part><separator><decimal-part>. A precision modifier can be used as %n.mf, where n is the number of digits in the integer parte, and m is the number of digits in the decimal part.
    When %F is used, the separator character is always a dot character “.”. However, when %f is used, the separator character is the separator for the active locale. For instance, is the locale is set as LC_NUMERIC=es_ES, the separator character becomes a comma “,”.
  • %e, %E – Generate the scientific notation representation of a floating point number, using the lowercase “e” or uppercase “E” character
  • %g, %G – Represent the number in normal notation (same as %f, %F) for small numbers, and in scientific notation (%e, %E) for large numbers.


The statements:

$num1 = 1234.6789;
printf("Using spanish separator: %.2f, using english separator: %.2F\n", $num1, $num1);
printf("Scientific notation: %.2e, with %%g specifier: %g\n", $num1, $num1);

Generan como salida:

Using spanish separator: 1234,68, using english separator: 1234.68
Scientific notation: 1.23e+3, with the %g specifier: 1234,68

As we can see in this example, setlocale() can be used to specify the locale. Using spanish locale, the %f specifier prints the number a comma as separator, but the %F specifier continues using a dot. Also the %g specifier uses a comma, because the number is small, and in this case it acts the same as %f

Finally, we can also appreciate that both %f and %F round the number if the number of decimal digits printed is smaller than the number of decimal digits in the number.

Using number_format() to format with a custom miliard separator

number_format() is a built-in funtion in PHP to apply a miliard separator character. This function can use one, two or four arguments:

string number_format ( float $number )
string number_format ( float $number, int $decimals)
string number_format ( float $number , int $decimals, string $dec_sep, string $mil_sep )

In the first case, number_format just returns the integer part of the number

In the second case, the second argument specifies the number of digits desired in the decimal part

Finally, number_format can also accept two additional arguments with the decimal separator character and miliard separator character to use.


The sentences:

$number = 1234567.1234567;
echo number_format($number) . "\n";
echo number_format($number , 5) . "\n";
echo number_format($number , 2 , "," , ".") . "\n";

Generate the following output:



php.net – Function sprintf

php.net – Function number_format

 Posted by at 9:01 pm

 Leave a Reply