Statistics

Problem Statement for "CutoffRounder"

Problem Statement

Often, when we round a real valued number to an integer, we round up if the fractional part is 0.5 or greater, and down if the fractional part is less than 0.5. In this problem, you are to write a method round, which takes a real valued number as a String, num, and a cutoff as a String, cutoff. cutoff will be formatted exactly as "0.####", where each '#' represents a digit ('0'-'9'). At least one of the digits to the right of the decimal point in cutoff will be non-zero. Your task is to round num up if its fractional part is greater than cutoff, and down otherwise, and return the result as an int. To avoid issues with double imprecision, the fractional part of num will not be exactly equal to cutoff

Hence, the traditional rounding method described in the opening sentence would be represented by cutoff = "0.5000"

Definition

Class:
CutoffRounder
Method:
round
Parameters:
String, String
Returns:
int
Method signature:
int round(String num, String cutoff)
(be sure your method is public)

Constraints

  • cutoff will be formatted exactly as "0.####", where each '#' represents a digit ('0'-'9').
  • num will be a sequence of one or more digits ('0'-'9'), with an optional decimal point ('.').
  • num will contain between 1 and 10 characters, inclusive.
  • The fractional part of num will not be exactly equal to cutoff.
  • num will be between 0 and 2,000,000,000, inclusive.

Examples

  1. "003.656930"

    "0.5000"

    Returns: 4

    0.65693 is greater than 0.5000, so we round up.

  2. ".001"

    "0.0001"

    Returns: 1

    A very low cutoff.

  3. "1.99999999"

    "0.9999"

    Returns: 2

  4. "135"

    "0.6531"

    Returns: 135

  5. "135."

    "0.6531"

    Returns: 135

  6. "1356.13671"

    "0.1367"

    Returns: 1357

  7. "0.00010001"

    "0.0001"

    Returns: 1

  8. "8.09871845"

    "0.7686"

    Returns: 8

  9. "3.08395116"

    "0.0644"

    Returns: 4

  10. "5.92313529"

    "0.4448"

    Returns: 6

  11. "2.78002571"

    "0.6646"

    Returns: 3

  12. "2.06727476"

    "0.6701"

    Returns: 2

  13. "4.12000643"

    "0.9880"

    Returns: 4

  14. "7.00141667"

    "0.0378"

    Returns: 7

  15. "4.01010601"

    "0.1206"

    Returns: 4

  16. "3.37102613"

    "0.7196"

    Returns: 3

  17. "6.07413963"

    "0.2014"

    Returns: 6

  18. "6.95004621"

    "0.7310"

    Returns: 7

  19. "3.23409438"

    "0.5293"

    Returns: 3

  20. "1.10098438"

    "0.6938"

    Returns: 1

  21. "7.92270291"

    "0.5631"

    Returns: 8

  22. "5.41414392"

    "0.2424"

    Returns: 6

  23. "4.27141959"

    "0.8894"

    Returns: 4

  24. "9.17186083"

    "0.2145"

    Returns: 9

  25. "2.42622954"

    "0.0441"

    Returns: 3

  26. "6.32255295"

    "0.7484"

    Returns: 6

  27. "1.88850429"

    "0.7432"

    Returns: 2

  28. "6.02679408"

    "0.3743"

    Returns: 6

  29. "5.39233266"

    "0.6461"

    Returns: 5

  30. "9.27097782"

    "0.3541"

    Returns: 9

  31. "7.45833598"

    "0.3319"

    Returns: 8

  32. "1.10892893"

    "0.6692"

    Returns: 1

  33. "1.29535300"

    "0.2227"

    Returns: 2

  34. "1.57579983"

    "0.1045"

    Returns: 2

  35. "4.02989128"

    "0.1629"

    Returns: 4

  36. "5.99280642"

    "0.8032"

    Returns: 6

  37. "5.88131910"

    "0.9851"

    Returns: 5

  38. "0.44"

    "0.5000"

    Returns: 0

  39. ".0005"

    "0.5000"

    Returns: 0

  40. ".001"

    "0.0001"

    Returns: 1

  41. "135"

    "0.6531"

    Returns: 135

  42. "1.000001"

    "0.0001"

    Returns: 1

  43. "1000000"

    "0.0001"

    Returns: 1000000

  44. "3.0002"

    "0.5000"

    Returns: 3

  45. "0.012"

    "0.0210"

    Returns: 0


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: