php sprintf


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.

3.43
7

                                    &lt;?php
$num = 5;
$location = 'tree';

$format = 'There are %d monkeys in the %s';
echo sprintf($format, $num, $location);

3.43 (7 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
php sprintf put % $squery sprintf php php sprintf interger Sprintf complete with char php sprintf %0 php what does sprintf() function do in php php sprintf %1$s php new process sprintf sprintf trng php php sprintf('%04d', example php sprintf int php sprintf number php sprintf % sprintf php intewger sprintf(%02d) php sprintf in php and javscript php sprintf('+%s sprintf const php printf vs sprintf php php formato sprintf php sprintf in sprintf php sprintc what is sprintf function in php php sprintf object sprintf php any value echo php in sprintf function sprintf php php printf vs sprintf %16s sprintf php php sprintd sprintf php format sprintf syntax php php sprintf year echo sprintf php php dynamic sprintf php string sprintf php sprintg php sprintf g sprintf %c php php mysql sprintf sprintf within sptrintf php sprintf function in php example for sprintf php printf vs sprintf in php sprintf in variable php sprintf interger php character in sprintf php printf et sprintf php php sprintf example php printf %X with variables sprintf php integer php sprintf date string formatting php sprint_f php sprintf in laravel return sprintf php sprint php %s sprint f php laravel sprintf sprintif blade sprint f string numeric placeholders format php is format php string sprintf variables %d %s sprintf 02d php %.2f php php sprintf() can sprintf be a param of sprintf can sprintf be a param of sprint php springf %02d%02d php php long formatted text inside variable %d php php sprint f sprintf('%04d', $i) php potencias string format php decinal placeholder 2%f + php cho sprintf() php php sprintf format loop format string character 5 numbers php %s %d php sprintf date php format() %s php format(%s) php integer to string format sprintf example php php sprintf numbering Change the currency in the price placeholder in php file Change the currency in the price place holder in php file similar functions as sprintf in php function to use instead of sprintf in php sprintf php %04X formated string php &quot;sprintf&quot; force lowercase sprintf format boolean php what is sprintf in php sprintf float php php number string format php number format %02d angularjs sprintf %08d like php php sprintf generator formatting variable function in php php %f sprintf %1 php php sprintf definition php sprintf( 01.2f formatting variables php formatting variables in php sprintf prints numbers php format string di php special formating php string.format php string.format in php php sprintf shortcut php %d php string formatting php sprintf boolean var php string formating is sprintf archaic php format text php php sprintf query php sfprint php %s in string php echo variable format php sprintf variable index sprintf( php define string format Sprintf %1$s PHP sprintf in php pdf sprintf in php mysql deduct in sprintf in php mysql sum function in sprintf in php mysql sprintf php calculation sprintf datetime php php %1$s how to use sprintf in php calculation inside sprintf function in php substraction inside sprintf function in php php number to string format php string now to format hexadecimal specifier for uppercase in php %s php string formatt in php sprintf formating in php php printf string padding sprintf function in php php echo string format php sprintf function sprintf euro format php print fix how to format in php string and var php sprintf add php prepare (sprintf( function what is php sprintf php sprintf conditional format php sprintf\ php sprintf cheat sheet 1%s$ printf placeholder php sprintf format specifiers php format string php string format php php string format php $ string format sprintf %02s in php is sprintf %02s php string format in php php printf to string %s - %s php php sprontf sprint php sprintf() php print %s in php php sprint sprintf php example php formatted string &lt;&lt;&lt; php formatted string php format strings sprintf int php sprintf string php sptrinf int php 01.2f in php php string foman print_f in php sprintf laravel fromatted strings php php format text php sprintffloat php return formatted string php formatted string % sprintf in php %1$s in php php format string %s in php sprintf php sprintf integer sprintf php cls with named parameters use optional values in sprintf+php use optial+sprintf+php sprintf php string repeat and format to right align in php revert back of sprintf in php how to make original from sprintf in php php sprintf
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source