HTML and CSS Reference
Table 10-25 (continued)
if (formatDollars.substring(0,1) == “,”)
dollars = formatDollars.substring(1,formatDollars.length)
dollars = formatDollars
Because the dollar portion of the value is still a string, line 79 uses the length
property to determine how many digits are in the dollar portion. The if statement at
line 80 determines if the dollar portion of the value is longer than three digits. If that is
true, then a while loop routine (lines 81 through 90) places a comma every three digits.
Line 82 extracts three digits starting from the right by subtracting 3 from the length of
the dollar value (dollarLen). The if statement at Line 83 verifies three digits and line 84
inserts a comma in the output string. Line 85 decrements the length of the dollar value
to look for the next group of three digits. When no more groups of three digits exist, the
length of dollarLen is set to zero (line 88) and the loop terminates at line 90. The state-
ments in lines 91 through 95 prevent the code from inserting a comma if only three digits
are to the left of the decimal point.
To Enter an if...else Statement and while Loop to Extract the Dollar Portion
of the Output and Insert Commas
The following step enters the if…else statement and while loop to extract the dollar portion of the output and
insert commas into the output if needed.
If necessary, click
determines number of digits in
dollar amount for placement
of commas every 3 digits
code as shown in
Table 10-25 starting
on the previous page,
indenting as shown
to extract the dollar
portion of the output,
insert the appropriate
commas, and then
press the e n t e r key
determines if commas
should be placed
every three digits, and
strips off any
Reconstructing the Formatted Output and Returning the Formatted
the formatted dollars and cents output into a formatted payment amount value, store the
payment amount value in the formatValue variable, and return the formatValue variable.
Table 10-26 shows the statements needed to complete this task.