note 42909 deleted from function.printf by tomsommer
| From: | tomsommer@php.net | Date: | Sat, 10 Jul 2004 00:02:43 +0000 |
| Subject: | note 42909 deleted from function.printf by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72948@lists.php.net to get a copy of this message | ||
Note Submitter: arman@media.berkeley.edu
----
In cases where you need to store the formatted string in a variable instead of outputting it, use
the number format (http://www.php.net/manual/en/function.number-format.php) function.
For example:
<?php
$a = 5; $b = 8.1; $c = 9.99;
print "Here comes the conversion: ";
$new_a = number_format($a, 2);
$new_b = number_format($b, 2);
$new_c = number_format($c, 2);
print "a is now =".$new_a.", b is now = ".$new_b.", and c is now =
".$new_c;
?>
// output:
Here comes the conversion: a is now =5.00, b is now = 8.10, and c is now = 9.99
Whereas if you used something like:
<?php
$a = 5; $b = 8.1; $c = 9.99;
print "Here comes the conversion: ";
$new_a = printf("%.2f", $a);
$new_b = printf("%.2f", $b);
$new_c = printf("%.2f", $c);
print "a is now =".$new_a.", b is now = ".$new_b.", and c is now =
".$new_c;
?>
// you would get this:
Here comes the conversion: 5.008.109.99a is now =, b is now = , and c is now =
That's because printf sends the strings to output immediatly and does not assign its output to
variables.