Statistics

Problem Statement for "FormatAmt"

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.

Definition

Class:
FormatAmt
Method:
amount
Parameters:
int, int
Returns:
String
Method signature:
String amount(int dollars, int cents)
(be sure your method is public)

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

  1. 123456

    0

    Returns: "$123,456.00"

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

  2. 49734321

    9

    Returns: "$49,734,321.09"

  3. 0

    99

    Returns: "$0.99"

    Note the leading 0.

  4. 249

    30

    Returns: "$249.30"

  5. 2

    2

    Returns: "$2.02"

  6. 29

    69

    Returns: "$29.69"

  7. 1000

    1

    Returns: "$1,000.01"

  8. 99999

    10

    Returns: "$99,999.10"

  9. 2000000000

    55

    Returns: "$2,000,000,000.55"

  10. 3040506

    0

    Returns: "$3,040,506.00"

  11. 0

    0

    Returns: "$0.00"

  12. 0

    5

    Returns: "$0.05"

  13. 205

    6

    Returns: "$205.06"

  14. 20502030

    70

    Returns: "$20,502,030.70"

  15. 1001001001

    1

    Returns: "$1,001,001,001.01"

  16. 600

    00

    Returns: "$600.00"

  17. 12345

    0

    Returns: "$12,345.00"

  18. 1010001

    1

    Returns: "$1,010,001.01"

  19. 0

    0

    Returns: "$0.00"

  20. 100000

    44

    Returns: "$100,000.44"

  21. 324567789

    54

    Returns: "$324,567,789.54"

  22. 29

    29

    Returns: "$29.29"

  23. 1000

    1

    Returns: "$1,000.01"

  24. 1000

    99

    Returns: "$1,000.99"

  25. 1234

    1

    Returns: "$1,234.01"

  26. 7

    7

    Returns: "$7.07"

  27. 0

    98

    Returns: "$0.98"

  28. 12345

    6

    Returns: "$12,345.06"

  29. 21

    0

    Returns: "$21.00"


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: