R/nice.R

Defines functions nice

Documented in nice

#' nice
#'
#' Formats numbers to the preferred format: X.XX
#' Takes a number or a vector
#' @param x number of vector of numbers
#' @return Vector of strings
#' @export
#' @examples
#' x <- c(.200, .123, 2.2); nice(x)

nice <- function(x) {

  if (length(x) > 1) {
    temp <- c()
    for (i in 1:length(x)){
      temp[i] <- nice(x[i])
    }
    return(temp)
  }

  temp <-  sprintf("%1.2f", as.numeric(x))

  if (as.numeric(temp) == 0) temp <- "0.00"
  splits <- strsplit(temp, "\\.")[[1]]

  if (splits[1] == "0")  temp <- paste(".", splits[2], sep = "")

  if (splits[1] == "-0") temp <- paste("-.", splits[2], sep = "")
  temp
}
soodoku/goji documentation built on May 30, 2019, 6:29 a.m.