hiết lập độ chính xác Không chỉ bạn có thể chỉ định một loại chuyển đổi, bạn cũng có thể thiết lập độ chính xác của kết quả hiển thị. Ví dụ, các khoản tiền thường được hiển thị với hai chữ số của độ chính xác. | Precision Setting Not only can you specify a conversion type you can also set the precision of the displayed result. For example amounts of currency are usually displayed with only two digits of precision. However after a calculation a value may have a greater precision than this such as 12 which results in . To ensure that such values are correctly stored internally but displayed with only two digits of precision you can insert the string .2 between the symbol and the conversion specifier printf The result is .2f 12 The output from this command is The result is But you actually have even more control than that because you can also specify whether to pad output with either zeros or spaces by prefacing the specifier with certain values. Example 7-1 shows five possible combinations. Example 7-1. Precision setting php echo pre Enables viewing of the spaces Pad to 15 spaces printf The result is 15f n 12 Pad to 15 spaces fill with zeros printf The result is 015f n 12 Pad to 15 spaces 2 decimal places precision printf The result is n 12 Pad to 15 spaces 2 decimal places precision fill with zeros printf The result is n 12 Pad to 15 spaces 2 decimal places precision fill with symbol printf The result is n 12 The output from this example looks like this The result is The result is The result is The result is The result is The way it works is simple if you go from right to left see Table 7-2 . Notice that The rightmost character is the conversion specifier. In this case it is f for floating point. Just before the conversion specifier if there is a period and a number together then the precision of the output is specified as the value of the number. Using printf 131 Regardless of whether there s a precision specifier if there is a number then that represents the amount of characters to which the output should be padded. In the previous example