HTML and CSS Reference
in Monthly Payment
Formatting output results.
To format the result, the dollarFormat() function performs these seven basic steps:
1. Initialize two variables: one to hold the dollars portion of the value and the other to
hold the formatted amount
2. Separate the dollars portion from the cents portion by using the split() method
3. The split() method creates an array; the zero element contains the dollar amount
4. Insert commas every three positions in dollar amounts exceeding 999
5. Reconstruct the result string value with two decimal places
6. Insert a dollar sign immediately to the left of the first digit without spaces
7. Return the completed formatted value
The value is assigned to the Monthly Payment text field in the form.
Formatting the Monthly Payment Output as Currency
As shown in Figure 10-26, the loan payment calculator currently displays the
monthly payment amount as a value with two decimal places. To set the form to display
the monthly payment amount in a currency format with a dollar sign, the dollarFormat()
function is used. First, you must enter a statement that passes the resulting string object
of the monthly payment CalcLoanAmt() function to the dollarFormat() function. The
dollarFormat() function then analyzes the string, adds commas, and returns the number
with a dollar sign and two decimal places.