R/xP.R

Defines functions xP

Documented in xP

# express x in terms of units
# includes conversion to dollars
xP <- function(x, d_d=NULL, unit=NULL, semi=FALSE) {

  if (is.null(d_d))
    digits_d <- getOption("digits_d")
  else
    digits_d <- d_d

  if (!is.na(x)) {

    neg.flag <- FALSE
    if (!is.null(unit)) {
      if (unit %in% c("dollar", "dollars")){
        digits_d <- 2
        if (x < 0) {
          neg.flag <- TRUE
          x <- abs(x)  # remove minus sign
        }
      }
    }

    tx <- formatC(x, digits=digits_d, big.mark=",", format="f")

    if (!is.null(unit)) {
      if (unit != "dollar") {  # No $ for display of the value
        if (!semi)
          tx <- paste(tx, unit)
        else
          tx <- paste(tx, "\\:", unit)
      }
      else {
        if (!neg.flag)
          tx <- paste("$", tx, sep="")
        else  # Had removed minus sign, now add back in the front
          tx <- paste("-$", tx, sep="")
        }
    }
    
  }  # end not missing x

  else
    tx <- ""

  return(tx)

}

Try the lessR package in your browser

Any scripts or data that you put into this service are public.

lessR documentation built on June 23, 2024, 1:06 a.m.