When provided a string representing a fraction, parse_fraction
will parse
the provided string to create a list
with the class fraction
applied to
it. This will allow the parsed fraction to be used in subsequent calculations.
parse_fraction(string, improper = TRUE, reduce = TRUE)
string | The input character to be parsed. |
---|---|
improper | Logical. Should the result be kept as an improper fraction?
Defaults to |
reduce | Logical. Should unreduced fractions be simplified? Defaults
to |
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 0
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.
The string can be entered either as an improper fraction
(for example, "5/2"
) or as a simplified fraction (for example,
"2 1/2"
). Depending on how it is entered, the resulting list
will have a value in "whole"
or "whole"
will be NULL
.
parse_fraction("4/4")#> [1] "1"parse_fraction("4/4", reduce = FALSE)#> [1] "4/4"parse_fraction("32/4")#> [1] "8"parse_fraction("34/4", reduce = FALSE)#> [1] "34/4"parse_fraction("34/4", reduce = TRUE)#> [1] "17/2"parse_fraction("34/4", improper = FALSE)#> [1] "8 1/2"parse_fraction("4 2/4")#> [1] "9/2"parse_fraction("4 2/4", TRUE, FALSE)#> [1] "18/4"parse_fraction("4 2/4", FALSE)#> [1] "4 1/2"