将高位数字四舍五入到 10k 的一半(15k 和 25k)
Round up high digit numbers to halfway 10k (15k and 25k)
我正在尝试将以下高位四舍五入为特定值。
lowboundx = 14400
我目前使用以下任一代码行,但它似乎总是将其四舍五入到 10k 之类的值。
seq1x <- round(lowboundx, digits = -(trunc(log10(abs(lowboundx))))) #outcome = 10000
seq2x <- round(lowboundx, digits = - 4) #outcome = 10000
我需要的是将这个和任何其他数字四舍五入到最接近的整数(而不是向下)。例如:
9999 becomes 10000 | but | 10001 becomes 15000
11000 becomes 15000
14400 becomes 15000
17760 becomes 20000 and so on...
我知道 trunc 被用来将事情向下舍入到下一个逻辑数字,但是有没有办法扭转它并让它上升?
欢迎任何帮助!
my_numbers <- c(9999, 11000, 14400, 17760)
ceiling(my_numbers / 5000) * 5000
#[1] 10000 15000 15000 20000
以下假设您有整数,并且想要第一个或两个有效数字
x <- c(9999, 10000, 10001, 11000, 14400, 17760)
digits <- floor(log10(x)) + 1
scale_factor <- 5 * 10^(pmax(0, digits-2))
scale_factor*ceiling(x/scale_factor )
#10000 10000 15000 15000 15000 20000
我正在尝试将以下高位四舍五入为特定值。
lowboundx = 14400
我目前使用以下任一代码行,但它似乎总是将其四舍五入到 10k 之类的值。
seq1x <- round(lowboundx, digits = -(trunc(log10(abs(lowboundx))))) #outcome = 10000
seq2x <- round(lowboundx, digits = - 4) #outcome = 10000
我需要的是将这个和任何其他数字四舍五入到最接近的整数(而不是向下)。例如:
9999 becomes 10000 | but | 10001 becomes 15000
11000 becomes 15000
14400 becomes 15000
17760 becomes 20000 and so on...
我知道 trunc 被用来将事情向下舍入到下一个逻辑数字,但是有没有办法扭转它并让它上升? 欢迎任何帮助!
my_numbers <- c(9999, 11000, 14400, 17760)
ceiling(my_numbers / 5000) * 5000
#[1] 10000 15000 15000 20000
以下假设您有整数,并且想要第一个或两个有效数字
x <- c(9999, 10000, 10001, 11000, 14400, 17760)
digits <- floor(log10(x)) + 1
scale_factor <- 5 * 10^(pmax(0, digits-2))
scale_factor*ceiling(x/scale_factor )
#10000 10000 15000 15000 15000 20000