Connection Status:
Competition Arena > FormatAmt
SRM 149 · 2003-06-02 · by dgoodman · String Manipulation
Class Name: FormatAmt
Return Type: String
Method Name: amount
Arg Types: (int, int)
Problem Statement

Problem Statement

In documents, it is frequently necessary to write monetary amounts in a standard format. We have decided to format amounts as follows:
  1. the amount must start with '$'
  2. the amount should have a leading '0' if and only if it is less then 1 dollar.
  3. the amount must end with a decimal point and exactly 2 following digits.
  4. the digits to the left of the decimal point must be separated into groups of three by commas (a group of one or two digits may appear on the left).

Create a class FormatAmt that contains a method amount that takes two int's, dollars and cents, as inputs and returns the properly formatted String.

Notes

  • One dollar is equal to 100 cents.

Constraints

  • dollars will be between 0 and 2,000,000,000 inclusive
  • cents will be between 0 and 99 inclusive
Examples
0)
123456
0
Returns: "$123,456.00"

Note that there is no space between the $ and the first digit.

1)
49734321
9
Returns: "$49,734,321.09"
2)
0
99
Returns: "$0.99"

Note the leading 0.

3)
249
30
Returns: "$249.30"
4)
2
2
Returns: "$2.02"

Submissions are judged against all 29 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class FormatAmt with a public method string amount(int dollars, int cents) · 29 test cases · 2 s / 256 MB per case

Submitting as anonymous