Floor and ceiling with 2 or more significant digits

You can round results to two significant digits using signif:

> signif(12500,2)
[1] 12000
> signif(12501,2)
[1] 13000

But are there any convenient features like dummy functions below signif.floorand signif.ceilingso that I can get two or more significant numbers with a floor or ceiling?

> signif.ceiling(12500,2)
[1] 13000
> signif.floor(12501,2)
[1] 12000

EDIT:

The current function signifworks with negative numbers and decimal numbers.

Therefore, a possible solution will preferably work with negative numbers:

> signif(-125,2)
[1] -120
> signif.floor(-125,2)
[1] -130

and decimal numbers:

> signif(1.23,2)
[1] 1.2
> signif.ceiling(1.23,2)
[1] 1.3

As a special case, also 0 should return 0:

> signif.floor(0,2)
[1] 0
+4
source share
2 answers

I think this approach is suitable for all types of numbers (i.e. integers, negative, decimal).

Gender function

signif.floor <- function(x, n){
  pow <- floor( log10( abs(x) ) ) + 1 - n
  y <- floor(x / 10 ^ pow) * 10^pow
  # handle the x = 0 case
  y[x==0] <- 0
  y
}

Ceiling function

signif.ceiling <- function(x, n){
  pow <- floor( log10( abs(x) ) ) + 1 - n
  y <- ceiling(x / 10 ^ pow) * 10^pow
  # handle the x = 0 case
  y[x==0] <- 0
  y
}

. , floor/ceiling. , .

1 x = 0, .

2 :

x

# for negative values
> values <- -0.12151 * 10^(0:4); values
# [1]    -0.12151    -1.21510   -12.15100  -121.51000 -1215.10000
> sapply(values, function(x) signif.floor(x, 2))
# [1]    -0.13    -1.30   -13.00  -130.00 -1300.00
> sapply(values, function(x) signif.ceiling(x, 2))
# [1]    -0.12    -1.20   -12.00  -120.00 -1200.00

# for positive values
> sapply(-values, function(x) signif.floor(x, 2))
# [1]    0.12    1.20   12.00  120.00 1200.00
> sapply(-values, function(x) signif.ceiling(x, 2))
# [1]    0.13    1.30   13.00  130.00 1300.00

n

> sapply(1:5, function(n) signif.floor(-121.51,n))
# [1] -200.00 -130.00 -122.00 -121.60 -121.51
> sapply(1:5, function(n) signif.ceiling(-121.51,n))
# [1] -100.00 -120.00 -121.00 -121.50 -121.51
+4

, @storaged , , :

(, , )

signif.floor=function(x,n){
  if(x==0)(out=0)
  if(x%%round(x)==0 & sign(x)==1){out=as.numeric(paste0(el(strsplit(as.character(x),''))[1:n],collapse=''))*10^(nchar(x)-n)}
  if(x%%round(x) >0 & sign(x)==1){out=as.numeric(paste0(el(strsplit(as.character(x),''))[1:(n+1)],collapse=''))}
  if(x%%round(x)==0 & sign(x)==-1){out=(as.numeric(paste0(el(strsplit(as.character(x),''))[1:(n+1)],collapse=''))-1)*10^(nchar(x)-n-1)}
  if(x%%round(x) <0 & sign(x)==-1){out=as.numeric(paste0(el(strsplit(as.character(x),''))[1:(n+2)],collapse=''))-+10^(-n+1)}
  return(out)
}

signif.ceiling=function(x,n){
  if(x==0)(out=0)
  if(x%%round(x)==0 & sign(x)==1){out=(as.numeric(paste0(el(strsplit(as.character(x),''))[1:n],collapse=''))+1)*10^(nchar(x)-n)}
  if(x%%round(x) >0 & sign(x)==1){out=as.numeric(paste0(el(strsplit(as.character(x),''))[1:(n+1)],collapse=''))+10^(-n+1)}
  if(x%%round(x)==0 & sign(x)==-1){out=(as.numeric(paste0(el(strsplit(as.character(x),''))[1:(n+1)],collapse='')))*10^(nchar(x)-n-1)}
  if(x%%round(x) < 0 & sign(x)==-1){out=as.numeric(paste0(el(strsplit(as.character(x),''))[1:(n+2)],collapse=''))}
  return(out)
}
0

Source: https://habr.com/ru/post/1688927/


All Articles