Return to sprintf_f_2.phpt CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / standard / tests / strings |
1.1 misho 1: --TEST-- 2: sprintf %f #2 3: --INI-- 4: precision=14 5: --FILE-- 6: <?php 7: var_dump(sprintf("%.3F", 100.426)); 8: var_dump(sprintf("%.2F", 100.426)); 9: var_dump(sprintf("%d", 100.426)); 10: var_dump(sprintf("%d", 100.9)); 11: var_dump(sprintf("%o", 100.426)); 12: var_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'; 21: var_dump(sprintf($format, $num, $location)); 22: 23: /* example#2: Argument swapping */ 24: $format = 'The %s contains %d monkeys'; 25: var_dump(sprintf($format, $num, $location)); 26: 27: /* example#3: Argument swapping */ 28: $format = 'The %2$s contains %1$d monkeys'; 29: var_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.'; 34: var_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 42: var_dump(sprintf("%%b = '%b'", $n)); // binary representation 43: var_dump(sprintf("%%c = '%c'", $c)); // print the ascii character, same as chr() function 44: var_dump(sprintf("%%d = '%d'", $n)); // standard integer representation 45: var_dump(sprintf("%%e = '%e'", $n)); // scientific notation 46: var_dump(sprintf("%%u = '%u'", $n)); // unsigned integer representation of a positive integer 47: var_dump(sprintf("%%u = '%u'", $u)); // unsigned integer representation of a negative integer 48: var_dump(sprintf("%%f = '%f'", $n)); // floating point representation 49: var_dump(sprintf("%%o = '%o'", $n)); // octal representation 50: var_dump(sprintf("%%s = '%s'", $n)); // string representation 51: var_dump(sprintf("%%x = '%x'", $n)); // hexadecimal representation (lower-case) 52: var_dump(sprintf("%%X = '%X'", $n)); // hexadecimal representation (upper-case) 53: 54: var_dump(sprintf("%%+d = '%+d'", $n)); // sign specifier on a positive integer 55: var_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: 62: var_dump(sprintf("[%s]", $s)); // standard string output 63: var_dump(sprintf("[%10s]", $s)); // right-justification with spaces 64: var_dump(sprintf("[%-10s]", $s)); // left-justification with spaces 65: var_dump(sprintf("[%010s]", $s)); // zero-padding works on strings too 66: var_dump(sprintf("[%'#10s]", $s)); // use the custom padding character '#' 67: var_dump(sprintf("[%10.10s]", $t)); // left-justification but with a cutoff of 10 characters 68: 69: /* example#7: zero-padded integers */ 70: var_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; 76: var_dump(sprintf("%01.2f", $money)); // output "123.10" 77: 78: /* example#9: scientific notation */ 79: $number = 362525200; 80: 81: var_dump(sprintf("%.3e", $number)); // outputs 3.63e+8 82: ?> 83: --EXPECTREGEX-- 84: string\(7\) \"100\.426\" 85: string\(6\) \"100\.43\" 86: string\(3\) \"100\" 87: string\(3\) \"100\" 88: string\(3\) \"144\" 89: string\(3\) \"144\" 90: string\(34\) \"There are 100 monkeys in the world\" 91: string\(28\) \"The 100\.1 contains 0 monkeys\" 92: string\(30\) \"The world contains 100 monkeys\" 93: string\(76\) \"The world contains 100 monkeys. 94: That's a nice world full of 100 monkeys\.\" 95: string\(33\) \"%b = '10100111101010011010101101'\" 96: string\(8\) \"%c = 'A'\" 97: string\(15\) \"%d = '43951789'\" 98: string\(18\) \"%e = '4\.395179e\+7'\" 99: string\(15\) \"%u = '43951789'\" 100: (string\(17\) \"%u = '4251015507'\"|string\(27\) \"%u = '18446744073665599827'\") 101: string\(22\) \"%f = '43951789\.000000'\" 102: string\(16\) \"%o = '247523255'\" 103: string\(15\) \"%s = '43951789'\" 104: string\(14\) \"%x = '29ea6ad'\" 105: string\(14\) \"%X = '29EA6AD'\" 106: string\(17\) \"%\+d = '\+43951789'\" 107: string\(17\) \"%\+d = '-43951789'\" 108: string\(8\) \"\[monkey\]\" 109: string\(12\) \"\[ monkey\]\" 110: string\(12\) \"\[monkey \]\" 111: string\(12\) \"\[0000monkey\]\" 112: string\(12\) \"\[####monkey\]\" 113: string\(12\) \"\[many monke\]\" 114: string\(10\) \"2006-12-18\" 115: string\(6\) \"123\.10\" 116: string\(8\) \"3\.625e\+8\"