Added proportion2str

This commit is contained in:
Waldir Leoncio 2019-12-17 16:38:55 +01:00
parent be1d759fff
commit 11efa94ee8
6 changed files with 56 additions and 22 deletions

View file

@ -674,23 +674,4 @@ admix1 <- function(tietue) {
# fclose(fid);
# else
# diary off
# end
# %------------------------------------------------------
# function str = proportion2str(prob)
# %prob belongs to [0.00, 0.01, ... ,1].
# %str is a 4-mark presentation of proportion.
# if abs(prob)<1e-3
# str = '0.00';
# elseif abs(prob-1) < 1e-3;
# str = '1.00';
# else
# prob = round(100*prob);
# if prob<10
# str = ['0.0' num2str(prob)];
# else
# str = ['0.' num2str(prob)];
# end;
# end;
# end

21
R/proportion2str.R Normal file
View file

@ -0,0 +1,21 @@
#' @title Convert proportion to string
#' @param prob belongs to [0.00, 0.01, ... ,1]
#' @return a 4-mark presentation of proportion
#' @note The `round` function in R, being ISO-compliant, rounds 8.5 to 8. The
#' Matlab equivalent rounds it to 9.
#' @export
proportion2str <- function (prob) {
if (abs(prob) < 1e-3) {
str <- '0.00'
} else if (abs(prob - 1) < 1e-3) {
str <- '1.00'
} else {
prob <- round(100 * prob)
if (prob < 10) {
str <- paste0('0.0', as.character(prob))
} else {
str <- paste0('0.', as.character(prob))
}
}
return(str)
}