How to Round Up to the Nearest 10 (Or 100 or X)

How to round up to the nearest 10 (or 100 or X)?

If you just want to round up to the nearest power of 10, then just define:

roundUp <- function(x) 10^ceiling(log10(x))

This actually also works when x is a vector:

> roundUp(c(0.0023, 3.99, 10, 1003))
[1] 1e-02 1e+01 1e+01 1e+04

..but if you want to round to a "nice" number, you first need to define what a "nice" number is. The following lets us define "nice" as a vector with nice base values from 1 to 10. The default is set to the even numbers plus 5.

roundUpNice <- function(x, nice=c(1,2,4,5,6,8,10)) {
if(length(x) != 1) stop("'x' must be of length 1")
10^floor(log10(x)) * nice[[which(x <= 10^floor(log10(x)) * nice)[[1]]]]
}

The above doesn't work when x is a vector - too late in the evening right now :)

> roundUpNice(0.0322)
[1] 0.04
> roundUpNice(3.22)
[1] 4
> roundUpNice(32.2)
[1] 40
> roundUpNice(42.2)
[1] 50
> roundUpNice(422.2)
[1] 500

[[EDIT]]

If the question is how to round to a specified nearest value (like 10 or 100), then James answer seems most appropriate. My version lets you take any value and automatically round it to a reasonably "nice" value. Some other good choices of the "nice" vector above are: 1:10, c(1,5,10), seq(1, 10, 0.1)

If you have a range of values in your plot, for example [3996.225, 40001.893] then the automatic way should take into account both the size of the range and the magnitude of the numbers. And as noted by Hadley, the pretty() function might be what you want.

How to round up to nearest 10 or 100 or x

Please try this formula

System.out.println(Math.ceil(x*Math.pow(10, -Math.floor(Math.log10(x)))) / Math.pow(10, -Math.floor(Math.log10(x))) ) ;

You can try this formula online.

class Main {
public static void niceround(double x) {
System.out.println(x+" to " + Math.ceil(x * Math.pow(10, -Math.floor(Math.log10(x)))) / Math.pow(10, -Math.floor(Math.log10(x))));
}
public static void main(String[] args) {
niceround(0.0322);
niceround(3.22);
niceround(32.2);
niceround(42.2);
niceround(422.2);
}
}

test

/usr/lib/jvm/java-1.8.0-openjdk-amd64/bin/java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:36808,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/charsets.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/icedtea-sound.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/jaccess.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/jce.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/jsse.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/management-agent.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/resources.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/rt.jar:/home/dac/proj/javatest2016/out/production/javatest2016:/home/dac/Downloads/idea-IU-145.972.3/lib/idea_rt.jar Main
Connected to the target VM, address: '127.0.0.1:36808', transport: 'socket'
0.0322 to 0.04
3.22 to 4.0
32.2 to 40.0
42.2 to 50.0
422.2 to 500.0
Disconnected from the target VM, address: '127.0.0.1:36808', transport: 'socket'

Process finished with exit code 0

How to round up a number to nearest 10?

floor() will go down.

ceil() will go up.

round() will go to nearest by default.

Divide by 10, do the ceil, then multiply by 10 to reduce the significant digits.

$number = ceil($input / 10) * 10;

Edit: I've been doing it this way for so long.. but TallGreenTree's answer is cleaner.

Rounding numbers to nearest 10 in R

From ?round

Rounding to a negative number of digits means rounding to a power
of ten, so for example ‘round(x, digits = -2)’ rounds to the
nearest hundred.

So,

data <- c(152.335, 39.431, 21.894)
round(data, -1)
#[1] 150 40 20

Round Down to the Nearest 10

Since floor(x) gets the smallest integer y not greater than x, you can divide all the values in x by ten, get the floor, then multiply back by ten; i.e., you can use floor(x/10) * 10

x <- c(10,
15,
20,
27,
30,
34)
floor(x/10) * 10
# [1] 10 10 20 20 30 30

How to round an integer up or down to the nearest 10 using Javascript

Divide the number by 10, round the result and multiply it with 10 again, for example:

  1. 33 / 10 = 3.3
  2. 3.3 rounded = 3
  3. 3 × 10 = 30

console.log(Math.round(prompt('Enter a number', 33) / 10) * 10);

Round Values to Nearest Tens or Hundreds in R?

Maybe the following will do it.

x <- scan(text = "
71.00
55.3489
2000.11111
1689.66")
x

round_to <- function(x, to = 10) round(x/to)*to

round_to(x)
round_to(x, 100)

Edit.

After the comment by user ORStudent I have wrote a new function, roundup_to.

roundup_to <- function(x, to = 10, up = FALSE){
if(up) round(.Machine$double.eps^0.5 + x/to)*to else round(x/to)*to
}

roundup_to(c(150, 116350), to = 100)
# [1] 200 116400

roundup_to(c(150, 116350), to = 100, up = TRUE)
# [1] 200 116400

roundup_to(c(50, 116250), to = 100)
#[1] 0 116200

roundup_to(c(50, 116250), to = 100, up = TRUE)
#[1] 100 116300

Python - round up to the nearest ten

You can use math.ceil() to round up, and then multiply by 10

import math

def roundup(x):
return int(math.ceil(x / 10.0)) * 10

To use just do

>>roundup(45)
50

How to round up to the next 10?

I couldn't find an actual duplicate answer on Stack Overflow, so I am posting the following formula:

next_ten <- function(x) { 10*ceiling(x/10) }
v <- c(1, 5, 10, 11, 20, 29, 30, 31)
v
next_ten(v)

[1] 1 5 10 11 20 29 30 31
[1] 10 10 10 20 20 30 30 40

This tricks works by first finding how many tens, or decimal fractions of tens, there are (ceiling(x/10)). Then, we multiply by 10, to get the effective ceiling you want.

How can I round a number down to the nearest 10?

You could use TRUNCATE function as follow:

SELECT TRUNCATE(819, -1);
SELECT TRUNCATE(812, -1);

Result:
810


Related Topics



Leave a reply



Submit