1--TEST-- 2sprintf %f #2 3--INI-- 4precision=14 5--FILE-- 6<?php 7var_dump(sprintf("%.3F", 100.426)); 8var_dump(sprintf("%.2F", 100.426)); 9var_dump(sprintf("%d", 100.426)); 10var_dump(sprintf("%d", 100.9)); 11var_dump(sprintf("%o", 100.426)); 12var_dump(sprintf("%o", 100.9)); 13 14/* copy & paste from the docs */ 15 16/* example#1: Argument swapping */ 17$num = 100.1; 18$location = "world"; 19 20$format = 'There are %d monkeys in the %s'; 21var_dump(sprintf($format, $num, $location)); 22 23/* example#2: Argument swapping */ 24$format = 'The %s contains %d monkeys'; 25var_dump(sprintf($format, $num, $location)); 26 27/* example#3: Argument swapping */ 28$format = 'The %2$s contains %1$d monkeys'; 29var_dump(sprintf($format, $num, $location)); 30 31/* example#4: Argument swapping */ 32$format = 'The %2$s contains %1$d monkeys. 33 That\'s a nice %2$s full of %1$d monkeys.'; 34var_dump(sprintf($format, $num, $location)); 35 36/* example#5: various examples */ 37$n = 43951789; 38$u = -43951789; 39$c = 65; // ASCII 65 is 'A' 40 41// notice the double %%, this prints a literal '%' character 42var_dump(sprintf("%%b = '%b'", $n)); // binary representation 43var_dump(sprintf("%%c = '%c'", $c)); // print the ascii character, same as chr() function 44var_dump(sprintf("%%d = '%d'", $n)); // standard integer representation 45var_dump(sprintf("%%e = '%e'", $n)); // scientific notation 46var_dump(sprintf("%%u = '%u'", $n)); // unsigned integer representation of a positive integer 47var_dump(sprintf("%%u = '%u'", $u)); // unsigned integer representation of a negative integer 48var_dump(sprintf("%%f = '%f'", $n)); // floating point representation 49var_dump(sprintf("%%o = '%o'", $n)); // octal representation 50var_dump(sprintf("%%s = '%s'", $n)); // string representation 51var_dump(sprintf("%%x = '%x'", $n)); // hexadecimal representation (lower-case) 52var_dump(sprintf("%%X = '%X'", $n)); // hexadecimal representation (upper-case) 53 54var_dump(sprintf("%%+d = '%+d'", $n)); // sign specifier on a positive integer 55var_dump(sprintf("%%+d = '%+d'", $u)); // sign specifier on a negative integer 56 57 58/* example#6: string specifiers */ 59$s = 'monkey'; 60$t = 'many monkeys'; 61 62var_dump(sprintf("[%s]", $s)); // standard string output 63var_dump(sprintf("[%10s]", $s)); // right-justification with spaces 64var_dump(sprintf("[%-10s]", $s)); // left-justification with spaces 65var_dump(sprintf("[%010s]", $s)); // zero-padding works on strings too 66var_dump(sprintf("[%'#10s]", $s)); // use the custom padding character '#' 67var_dump(sprintf("[%10.10s]", $t)); // left-justification but with a cutoff of 10 characters 68 69/* example#7: zero-padded integers */ 70var_dump(sprintf("%04d-%02d-%02d", 2006, 12, 18)); 71 72/* example#8: formatting currency */ 73$money1 = 68.75; 74$money2 = 54.35; 75$money = $money1 + $money2; 76var_dump(sprintf("%01.2f", $money)); // output "123.10" 77 78/* example#9: scientific notation */ 79$number = 362525200; 80 81var_dump(sprintf("%.3e", $number)); // outputs 3.63e+8 82?> 83--EXPECTREGEX-- 84string\(7\) \"100\.426\" 85string\(6\) \"100\.43\" 86string\(3\) \"100\" 87string\(3\) \"100\" 88string\(3\) \"144\" 89string\(3\) \"144\" 90string\(34\) \"There are 100 monkeys in the world\" 91string\(28\) \"The 100\.1 contains 0 monkeys\" 92string\(30\) \"The world contains 100 monkeys\" 93string\(76\) \"The world contains 100 monkeys. 94 That's a nice world full of 100 monkeys\.\" 95string\(33\) \"%b = '10100111101010011010101101'\" 96string\(8\) \"%c = 'A'\" 97string\(15\) \"%d = '43951789'\" 98string\(18\) \"%e = '4\.395179e\+7'\" 99string\(15\) \"%u = '43951789'\" 100(string\(17\) \"%u = '4251015507'\"|string\(27\) \"%u = '18446744073665599827'\") 101string\(22\) \"%f = '43951789\.000000'\" 102string\(16\) \"%o = '247523255'\" 103string\(15\) \"%s = '43951789'\" 104string\(14\) \"%x = '29ea6ad'\" 105string\(14\) \"%X = '29EA6AD'\" 106string\(17\) \"%\+d = '\+43951789'\" 107string\(17\) \"%\+d = '-43951789'\" 108string\(8\) \"\[monkey\]\" 109string\(12\) \"\[ monkey\]\" 110string\(12\) \"\[monkey \]\" 111string\(12\) \"\[0000monkey\]\" 112string\(12\) \"\[####monkey\]\" 113string\(12\) \"\[many monke\]\" 114string\(10\) \"2006-12-18\" 115string\(6\) \"123\.10\" 116string\(8\) \"3\.625e\+8\" 117