The as_fraction function takes a numeric input and converts it to a fraction. The input is first rounded to the desired level of precision (entered as a value between 1 and 7).

as_fraction(number, precision = 3, improper = TRUE)

Arguments

number

The decimal you want to convert to a fraction.

precision

The number of digits to round the decimal to before trying to convert the result to a fraction. Must be greater than 1 but less than 8.

improper

Logical. Should the fraction be a returned as an improper fraction or a proper fraction? Defaults to TRUE.

Value

A formatted list printed with print.fraction(). The list includes four elements:

  • whole: The absolute value of the whole number part of the decimal. This is NULL if improper = TRUE.

  • numerator: The numerator of the resulting fraction.

  • denominator: The denominator of the resulting fraction.

  • sign: -1 if the input is negative; 1 if the input is positive; 0 if the input is zero.

Examples

as_fraction(3.2454)
#> [1] "649/200"
as_fraction(3.2454, 2, TRUE)
#> [1] "13/4"
as_fraction(3.2454, 2, FALSE)
#> [1] "3 1/4"
as_fraction(3.2454, 1, FALSE)
#> [1] "3 1/5"