Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Fraction to Recurring Decimal

XPChallenge Points: 20
levelLevel: Medium

You are given two integers — a numerator and a denominator — representing a mathematical fraction.
Your task is to return the decimal representation of that fraction as a string.

If the decimal part is repeating, you must enclose the repeating digits in parentheses.
If the fraction results in a finite decimal, return it as is (without parentheses).

Notes:
If there are multiple possible correct outputs, any valid one will be accepted.
The result string length will always be less than 10⁴ for all inputs.

Example 1:

Input: numerator = 3, denominator = 4

Output: "0.75"

Example 2:

Input: numerator = 10, denominator = 6

Output: "1.(6)"

Example 3:

Input: numerator = 7, denominator = 2

Output: "3.5"

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result