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:

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

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:

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:

 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:

produce the output:

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:

Print to screen:

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.

Example:

The statements:

Generan como salida:

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:

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.

Example:

The sentences:

Generate the following output:

References

php.net – Function sprintf

php.net – Function number_format

 Posted by at 9:01 pm

 Leave a Reply

(required)

(required)