Learn R Programming

exams.forge (version 1.0.10)

fcvt: Number to String Conversion (Floating Point / Fractional Number)

Description

Converts a number to a string containing either a floating point or a fractional number. Note that a repeating or recurring decimal, which is a number whose decimal representation becomes periodic, can also be expressed as a rational number. For example, \(\frac{1}{3}=0.333333333...=0.\overline{3}\). It is the workhorse used in num2str.

  • If denom is negative then always decimal point numbers are used (default).

  • If denom is zero then a mix of decimal point and fractional numbers are used (whatever is shorter).

  • If denom is one then fractional numbers are used except for integers.

  • If denom is larger than one, then the denominator is set to denom if possible.

Usage

fcvt(x, nsmall = 15, plus = FALSE, denom = -1)

Value

A character.

Arguments

x

numeric: numbers to convert

nsmall

integer: number of significant digits for the mantissa/significand (default: 16)

plus

logical: for positive numbers a plus sign should be used (default: FALSE)

denom

integer: denominator for a fractional number

Examples

Run this code
x1 <- c(NA, NaN, -Inf, Inf, 0, pi*10^(-20:20))
fcvt(x1)
x2 <- c(-0.36, 3.6, -30.6, 0.36)
fcvt(x2)
x3 <- c((0:16)/8, 1/3)
fcvt(x3)           # as floating point number, equals denom=-1
fcvt(x3, denom=0)  # as floating point or fractional number
fcvt(x3, denom=1)  # as fractional number except for integers
fcvt(x3, denom=8)  # as fractional number with denominator denom if possible

Run the code above in your browser using DataLab