how to get nearest tenth of a double

2020-04-11 03:11发布

问题:

I want to round up double number in java such that it converts to it's nearest tenth like below..

0.1--->0.1
0.3--->1
1----->1
1.5---->10
92---->100
4.0E8-->1.0E9
etc

How can I do it Actually my intention is to set Y-axis on chart, if max value is 0.1 then num_ spacing will set to .01 if it is .3 then convert to 1 then set num_ spacing to .1 and so on

回答1:

Try translating this into your language, I've written it in Matlab but it ought to be obvious

10^ceil(log10(x))

Of course, this will only work if x is positive.



回答2:

you can have a look..

              double a = 120.1;
    double last_digit_rem = 10 - (a % 10);
    System.out.println(a+last_digit_rem);

It will work for negative numbers also...



标签: java math