TopCoder Problem Statement - FormatAmt
Format a dollar amount according to a set of rules.
01: /* 02: TopCoder 03: Single Round Match: 149 04: Division: 2 05: Level: 1 06: Points: 250 07: Description: http://community.topcoder.com/stat?c=problem_statement&pm=1313 08: */ 09: 10: public class FormatAmt { 11: 12: /* 13: * Formats the dollars portion of the string 14: */ 15: private static String formatDollars(int d) { 16: 17: String s = String.valueOf(d); 18: 19: StringBuilder sb = new StringBuilder(); 20: 21: /* 22: * Starts from the digit neares the decimal point and works left 23: * toward the most significant digit. Inserts a comma after three 24: * characters, unless it's the final character. 25: */ 26: int count = 0; 27: for (int i = s.length() - 1; i >= 0; i--) { 28: 29: // Copies the current character to the front of the StringBuilder 30: sb.insert(0, s.charAt(i)); 31: count++; 32: 33: /* 34: * After 3 characters, insert a comma. But only if we're not now 35: * at the left-most position. 36: */ 37: if ((count == 3) && (i != 0)) { 38: sb.insert(0, ","); 39: count = 0; 40: } 41: } 42: return sb.toString(); 43: } 44: 45: /* 46: * Inserts leading 0's until the length is at least 2. 47: */ 48: private static String formatCents(int c) { 49: 50: String s = String.valueOf(c); 51: while (s.length() < 2) { 52: s = "0" + s; 53: } 54: 55: return s; 56: } 57: 58: /* 59: * Formats the dollars (to the left of the decimal point) and the cents 60: * separately, then combines them to make the return value. 61: */ 62: public String amount(int dollars, int cents) { 63: 64: return "$" + formatDollars(dollars) + "." + formatCents(cents); 65: } 66: }
The solution formats the dollars and cents portions separately, and then combines the together to form the return value.
Really the only tricky part is inserting the commas into the proper place in the dollars portion. This is handled by the for loop beginning at line 27. First, the variable count is initialized to 0. Then, the for loop works from right to left counting and inserting characters into a StringBuilder. When three characters have been seen, a comma is inserted, and count is set back to 0. If we're at the left-most position in the string, then the comma insertion is skipped.
Formatting the cents portion is even easier. There's a while loop that adds 0's on the beginning of the number until it's at least 2 wide.
Finally, the main method amount() combines the two, adding a "." between them, and prefixing the entire string with a "$"
No comments:
Post a Comment