#' Rounds the values in its first argument to the specified number of significant digits.
#'
#' Keeps "zeros" after decimal
#'
#' @param x Number to format
#' @param signifx Significant number
#' @param ... \code{format} arguments
#'
#' @keywords internal
#' @export
nsignif <- function(x, signifx, ...){
## Arrondit les valeurs dans son premier argument au nombre spécifié de chiffres significatifs.
## Comparativement à la fonction signif(), conserve les 'zéros' après la virgule
x <- signif(x, signifx)
# Nombre de décimales dans un nombre
num_decimals <- function (x){
if ((x%%1) != 0) {
nchar(strsplit(sub("0+$", "", as.character(x)), ".",
fixed = TRUE)[[1]][[2]])
}
else {
return(0)
}
}
decix <- num_decimals(x)
# Nombre de "chiffres" dans le nombre (nchar sans la décimale)
ndigits <- function(x){
nchar(sub('^0+','',sub('\\.','',x)))
}
ncharx <- ndigits(x)
# Détermine le nombre de charactères manquants
char <- signifx - ncharx
# Décimales à afficher
dshow <- char + decix
# Affichage du nombre
if(char>0){
format(signif(x, digits = signifx),
digits = dshow,
nsmall = dshow,
...)
} else {
format(signif(x, digits = signifx),
...)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.