0

I have the following function:

public static void main(String[] args) {
        int interval = 23950; 

        System.out.println (Math.round(weekInterval/1000.00)*1000);



    }

All the function does is rounding off the number (interval) to its nearest multiple of hundred or thousand etc.

Now the number interval is subject to change, it can vary from ranging between hundreds to billions, I want to set a general rule for division:

so that

Math.round(weekInterval/placeholder)*placeholder)

Any Ideas?

Edit:

So far this is what I have come up with, using my limited knowledge -- bear with me this might be inefficient:

public static void main(String[] args) {
        int weekInterval = 2500; 
        StringBuilder sbr = new StringBuilder(); 
        int len = String.valueOf(weekInterval).length();
        String num = "1"; 
        for(int i = 2; i<=len; i++){
            
            sbr.append("0"); 
        }
        System.out.println("Number is "+sbr.toString()); 
        System.out.println("Number is "+num+sbr.toString()); 
        
        int placeholder = Integer.valueOf(num+sbr.toString());
        System.out.println((weekInterval + placeholder/2)/placeholder*placeholder); 


    }
Community
  • 1
  • 1
User3
  • 2,465
  • 8
  • 41
  • 84
  • Not 100% sure what you are trying to accomplish. But could you use something like: int placeholder= Math.pow(10,(String.valueOf(interval).length()-2)); – Revive Jun 23 '14 at 09:59
  • ^^ I had come up with that in the beginning, but what if its a three digit number? the length()-2 part fails here. – User3 Jun 23 '14 at 10:01
  • Do you want to specifiy what it rounds up to? Hundreds or thousands etc? Or should the program always round to thousands etc? – Revive Jun 23 '14 at 10:10
  • That is the main issue, I cannot predict the length of the interval variable. It can range between a hundred to a billion. It should round based on the length of the interval variable. – User3 Jun 23 '14 at 10:11
  • For a 3 digit number. For example for 143 the code i gave will return 140. To its nearest ten. Is this incorrect? Or what should it return if so – Revive Jun 23 '14 at 10:17
  • It should return 100 for 140 and 200 for 150 – User3 Jun 23 '14 at 10:31
  • how about. double placeholder = String.valueOf(interval).length()>3 ? 1000.0 : 100.0; – Revive Jun 23 '14 at 10:45
  • Take into consideration a number greater than hundred thousand? The range of numbers is the main issue here. Another way would be to append trailing zeroes, based on the interval variable length. – User3 Jun 23 '14 at 10:48

2 Answers2

1

From http://mindprod.com/jgloss/round.html#MULTIPLE:

// rounding m up to next highest multiple of n
int ceil = ( m + n - 1 ) / n * n;

// rounding m down to multiple of n
int floor = m / n * n;

// rounding m to nearest multiple of n
int near = ( m + n/2 ) / n * n;

Here m is the number to round and n would be your 'placeholder'

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
0

A much easier solution using BigDecimal:

       BigDecimal interval = new BigDecimal("8953315756233454365754734");
       System.out.printf("%.0f\n",interval.round(new MathContext(1, RoundingMode.HALF_UP)));
Revive
  • 2,248
  • 1
  • 16
  • 23