PHP for the World Wide Web (Visual QuickStart Guide)
I l @ ve RuBoard |
Formatting Numbers
Although your calculator is on its way to being practical, you still have one legitimate problem: you cannot ask someone to make a monthly payment of $521.16666666667. In order to create a more usable number as your monthly payment, you will make your total cost an easily divisible number. To do this, you use the printf() function, which prints a formatted number according to your specifications. To use printf(), you feed it a format and a string (or number). For example, to print out an amount variable in the form of a floating-point number with two digits to the right of the decimal (e.g., 1.02 ), you would code: printf ("%01.2f", $Amount); The "%01.2f" segment tells PHP to print the $Amount using 0 to pad extra spaces, with at least one digit to the left of the decimal and with two digits to the right of the decimal. There are more complicated uses of printf(), but this will be more than sufficient for your need to make legitimate dollar amounts. To use printf():
Tip A parallel to the printf() function is the sprintf() function. It works the same only printf() will actually send the results to the browser whereas k sprintf() changes a variable without printing it. $Amount = sprintf ("%01.2f", $Amount);
|
I l @ ve RuBoard |