Online
 
Friday, 09 January 2009
 
 
More article:
Related Content:

Formatting Strings
 
Article Index
Formatting Strings
Page 2
Page 3
Page 4


Alternatives to printf()

Although the printf() function is extremely useful, it is also computationally intensive. As a result, you should try to limit its use as much as possible, relying instead on other functions provided by PHP for more specific tasks.

For example, you can use the number_format function to format a number according to a number of parameters:

number_format 
(
$number,
[$decimals,
[$point_separator,
$thousand_separator]]
);

The function works by formatting $number using at a minimum $decimals decimal digits, using $point_separator as a separator between the integer and decimal parts, and $thousand_separator to separate groups of thousands. If $decimals isn't specified, no decimal digits are shown. If $point_separator and $thousand_separator aren't used, the interpreter uses a dot (.) and a comma (,) in their place.

For example, in countries such as the U.K. and the United States, numbers are formatted using commas to separate the thousand groups, and dots are used to separate the integer part from the decimal part. Some European countries, such as Italy, use the opposite notation: dots separate the thousands and the comma indicates the beginning of the decimal part. Here's how number_format can be used to satisfy both requirements:

<?php

$a = 1232322210.44;

echo number_format ($a, 2); // English format
echo "\n";
echo number_format ($a, 2, ',', '.'); // Italian format
echo "\n";

?>

The preceding example produces the following output:

1,232,322,210.44
1.232.322.210,44


Tags: Add more tags...,
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: Simple PHP, Pear, Easy PHP, PHP Tutorial, PHP MySQL, XSLT, Sap Tutorial, CSS Tutorial, XSL FO Java, SQL Tutorial.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Mobile Wallpaper 16
Statistic


Last Post

 
Top! Top!