note 116776 added to function.sprintf

From: Date: Thu, 26 Feb 2015 10:34:32 +0000
Subject: note 116776 added to function.sprintf
Groups: php.notes 
Request: Send a blank email to php-notes+get-201813@lists.php.net to get a copy of this message
There are already some comments on using sprintf to force leading leading zeros but the examples only include integers. I needed leading zeros on floating point numbers and was surprised that it didn't work as expected. Example: <?php sprintf('%02d', 1); ?> This will result in 01. However, trying the same for a float with precision doesn't work: <?php sprintf('%02.2f', 1); ?> Yields 1.00. This threw me a little off. To get the desired result, one needs to add the precision (2) and the length of the decimal seperator "." (1). So the correct pattern would be <?php sprintf('%05.2f', 1); ?> Output: 01.00 Please see http://stackoverflow.com/a/28739819/413531 for a more detailed explanation. ---- Server IP: 72.52.91.14 Probable Submitter: 87.193.170.21 ---- Manual Page -- http://php.net/manual/en/function.sprintf.php Edit -- https://master.php.net/note/edit/116776 Del: integrated -- https://master.php.net/note/delete/116776/integrated Del: useless -- https://master.php.net/note/delete/116776/useless Del: bad code -- https://master.php.net/note/delete/116776/bad+code Del: spam -- https://master.php.net/note/delete/116776/spam Del: non-english -- https://master.php.net/note/delete/116776/non-english Del: in docs -- https://master.php.net/note/delete/116776/in+docs Del: other reasons-- https://master.php.net/note/delete/116776 Reject -- https://master.php.net/note/reject/116776 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#201813) next »