1

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

this link provides a solution to the problem in r, how can we achieve the similar thing in java?

it is not a case of simple rounding, but a bit complex rounding depending on the value itself, e.g. 0.0322 to 0.04, 3.22 to 4, 32.2 to 40, 42.2 to 50 and 422.2 to 500.

Community
  • 1
  • 1
Asif Altaf
  • 103
  • 1
  • 7

2 Answers2

3

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
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
1

I can not think of many reasons why the question was downvoted, especially because it was not said that "X" MUST be a potency of 10.

If your "X" is meant to be a potency of 10 you may find the Enum RoundingMode and the classes MathContext and BigDecimal helpful. For details please see https://docs.oracle.com/javase/8/docs/api/

Anyway: This Java method handles rounding int's for any rounding-base greater one:

int round(int num, 
          int base /* "X", MUST be greater than 1 */, 
          boolean roundMiddleUp /* true is usual in business calculations */) {
    int lower = (num / base) * base - (num < 0 ? base: 0);
    int upper = lower + base;

    int difLower = Math.abs(num - lower);
    int difUpper = Math.abs(num - upper);
    int rounded = (difLower < difUpper) ? lower : (difLower != difUpper) ? upper : roundMiddleUp ? upper : lower;
    return rounded;
}

Hope it helps.

Michael Besteck
  • 2,415
  • 18
  • 10
  • What matters is NOT what i THINK, but what is correct and what is the set of numbers Aisf Altaf need to compute on. Maybe it is also of importance what is the most teaching answer. I do not know, the questioneer needs to decide. – Michael Besteck Sep 08 '16 at 18:30
  • I think my formula (`Math.ceil(x*Math.pow(10, -Math.floor(Math.log10(x)))) / Math.pow(10, -Math.floor(Math.log10(x))) `) only works with potency of ten, but that java can have the `log` function for any base. – Niklas Rosencrantz Sep 08 '16 at 18:31