## DEV Community is a community of 867,901 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Dwayne Crooks

Posted on

# Elm Challenge - Write a function that outputs the decimal representation of a rational number

Every rational number can be represented as either a terminating or repeating decimal.

`1/2 = 0.5`, `1/4 = 0.25` and `1/5 = 0.2` are examples of terminating decimals.

And, `1/3 = 0.(3)`, `2/3 = 0.(6)`, `5/6 = 0.8(3)` and `3/7 = 0.(428571)` are examples of repeating decimals.

Write a function, `toDecimalString : Rational -> String`, that outputs the decimal representation of a given rational number.

Your function MUST pass the following tests:

``````toDecimalStringSuite : Test
toDecimalStringSuite =
describe "toDecimalString" <|
[ terminatingDecimalSuite
, repeatingDecimalSuite
]

terminatingDecimalSuite : Test
terminatingDecimalSuite =
describe "terminating decimals" <|
[ test "1/2" <|
\_ ->
Rational.new 1 2
|> Maybe.map Rational.toDecimalString
|> Expect.equal (Just "0.5")
, test "5/2" <|
\_ ->
Rational.new 5 2
|> Maybe.map Rational.toDecimalString
|> Expect.equal (Just "2.5")
, test "1/4" <|
\_ ->
Rational.new 1 4
|> Maybe.map Rational.toDecimalString
|> Expect.equal (Just "0.25")
, test "1/5" <|
\_ ->
Rational.new 1 5
|> Maybe.map Rational.toDecimalString
|> Expect.equal (Just "0.2")
, test "1/8" <|
\_ ->
Rational.new 1 8
|> Maybe.map Rational.toDecimalString
|> Expect.equal (Just "0.125")
]

repeatingDecimalSuite : Test
repeatingDecimalSuite =
describe "repeating decimals" <|
[ test "1/3" <|
\_ ->
Rational.new 1 3
|> Maybe.map Rational.toDecimalString
|> Expect.equal (Just "0.(3)")
, test "2/3" <|
\_ ->
Rational.new 2 3
|> Maybe.map Rational.toDecimalString
|> Expect.equal (Just "0.(6)")
, test "1/6" <|
\_ ->
Rational.new 1 6
|> Maybe.map Rational.toDecimalString
|> Expect.equal (Just "0.1(6)")
, test "5/6" <|
\_ ->
Rational.new 5 6
|> Maybe.map Rational.toDecimalString
|> Expect.equal (Just "0.8(3)")
, test "1/7" <|
\_ ->
Rational.new 1 7
|> Maybe.map Rational.toDecimalString
|> Expect.equal (Just "0.(142857)")
, test "2/7" <|
\_ ->
Rational.new 2 7
|> Maybe.map Rational.toDecimalString
|> Expect.equal (Just "0.(285714)")
, test "3/7" <|
\_ ->
Rational.new 3 7
|> Maybe.map Rational.toDecimalString
|> Expect.equal (Just "0.(428571)")
, test "1/9" <|
\_ ->
Rational.new 1 9
|> Maybe.map Rational.toDecimalString
|> Expect.equal (Just "0.(1)")
, test "2/9" <|
\_ ->
Rational.new 2 9
|> Maybe.map Rational.toDecimalString
|> Expect.equal (Just "0.(2)")
, test "8/9" <|
\_ ->
Rational.new 8 9
|> Maybe.map Rational.toDecimalString
|> Expect.equal (Just "0.(8)")
, test "7/12" <|
\_ ->
Rational.new 7 12
|> Maybe.map Rational.toDecimalString
|> Expect.equal (Just "0.58(3)")
, test "1/23" <|
\_ ->
Rational.new 1 23
|> Maybe.map Rational.toDecimalString
|> Expect.equal (Just "0.(0434782608695652173913)")
, test "1/97" <|
\_ ->
Rational.new 1 97
|> Maybe.map Rational.toDecimalString
|> Expect.equal (Just "0.(010309278350515463917525773195876288659793814432989690721649484536082474226804123711340206185567)")
]
``````