note 42909 added to function.printf

From: Date: Thu, 03 Jun 2004 09:42:10 +0000
Subject: note 42909 added to function.printf
Groups: php.notes 
Request: Send a blank email to php-notes+get-70401@lists.php.net to get a copy of this message
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. ---- Manual Page -- http://www.php.net/manual/en/function.printf.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+42909 Delete -- http://master.php.net/manage/user-notes.php?action=delete+42909&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+42909&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#70401) next »