user
This task has been flagged for clarification. Code on this page in its current state may be flagged incorrect once this task has been clarified. See this page's Talk page for discussion.

The task is to write a program to transform a decimal number into a fraction in lowest terms.

It is not always possible to do this exactly. For instance, while rational numbers can be converted to decimal representation, some of them need an infinite number of digits to be represented exactly in decimal form. Namely, repeating decimals such as 1/3 = 0.333...

Because of this, the following fractions cannot be obtained (reliably) unless the language has some way of representing repeating decimals:

* 67 / 74 = 0.9(054) = 0.9054054...
* 14 / 27 = 0.(518) = 0.518518...

Acceptable output:

* 0.9054054 → 4527027 / 5000000
* 0.518518 → 259259 / 500000

Finite decimals are of course no problem:

* 0.75 → 3 / 4
assistant
(formerly Perl 6)
Decimals are natively represented as rationals in Raku, so if the task does not need to handle repeating decimals, it is trivially handled by the .nude method, which returns the numerator and denominator:

```
say .nude.join('/') for 0.9054054, 0.518518, 0.75;

```

Output:

```
4527027/5000000
259259/500000
3/4
```

However, if we want to take repeating decimals into account, then we can get a bit fancier.

```
sub decimal_to_fraction ( Str $n, Int $rep_digits = 0 ) returns Str {
    my ( $int, $dec ) = ( $n ~~ /^ (\d+) \. (\d+) $/ )».Str or die;

    my ( $numer, $denom ) = ( $dec, 10 ** $dec.chars );
    if $rep_digits {
        my $to_move = $dec.chars - $rep_digits;
        $numer -= $dec.substr(0, $to_move);
        $denom -= 10 ** $to_move;
    }

    my $rat = Rat.new( $numer.Int, $denom.Int ).nude.join('/');
    return $int > 0 ?? "$int $rat" !! $rat;
}

my @a = ['0.9054', 3], ['0.518', 3], ['0.75', 0], | (^4).map({['12.34567', $_]});
for @a -> [ $n, $d ] {
    say "$n with $d repeating digits = ", decimal_to_fraction( $n, $d );
}

```

Output:

```
0.9054 with 3 repeating digits = 67/74
0.518 with 3 repeating digits = 14/27
0.75 with 0 repeating digits = 3/4
12.34567 with 0 repeating digits = 12 34567/100000
12.34567 with 1 repeating digits = 12 31111/90000
12.34567 with 2 repeating digits = 12 17111/49500
12.34567 with 3 repeating digits = 12 1279/3700
```