tags:

views:

195

answers:

8

Hi, I would like to build a method that outputs 1000 if the input number is thousands (eg. 3458), 100 if it is hundreds and so on. Is this possible? Please, sorry for my english.

+4  A: 

Of course it is possible. Why don't you post what you tried and we can give you pointers on how to solve any problems.

Steve McLeod
+4  A: 

As Steve said, it's probably a good idea to give it a little try yourself first, then come to SO with a specific question (i.e. "I am doing X, this is my code, why isn't Y happening?").

However, as a small hint, assuming you have pure numeric input (i.e. it will always just be a stream of numbers, with no ","s or the like) you can actually do the working out using just strings - no need for working with numerical types (int, etc) at all...

(Okay, thinking about this, there might be a little math right at the end to get the final result of '100' or '1000' etc, but not much.)

Stephen
+1  A: 

Simple math:

Math.pow(10, Math.floor(Math.log(n) / Math.log(10))) // for given n
Yuval A
Note: Works only for positive numbers.
Thorbjørn Ravn Andersen
Indeed, only positives.
Yuval A
A: 

Math.round(yourNumber/10)*10

Esteban
Sorry, wrong thread! :-)))
Esteban
+2  A: 

To expand on what Yuval offered, if you don't care about the sign of the number (that is, input values of, say, +3456 and -3456 should both return 1000), you can just use the absolute value of the input:

return Math.pow(10, Math.floor(Math.log( Math.abs(n) ) / Math.log(10))); // for input n

And if you want to handle all possible numeric inputs, you could also handle the zero-value before doing your calculation:

if (n == 0) // for input n
    return 0;
return Math.pow(10, Math.floor(Math.log( Math.abs(n) ) / Math.log(10))); // for input n

log(0) is undefined, so you don't want to perform the calculation if n == 0. You'll get a funny answer (if you even get an answer... I didn't run this code). Given the description of the problem you provided, I think returning 0 when the input is 0 makes sense. Zero isn't in the thousands or the hundreds or the tens or the ones -- among the integers, it is its own category. So you could return 0. Or you could throw an exception.

Matthew Whitrock
+1  A: 

A loop can be used to avoid using double and an eventual rounding problem (if transforming the result to int).
The loop variable starts with 1 and is multiplied by 10 each pass while the (next) result is less than the input number.
Negative and zero input need a special handling.

Carlos Heuberger
+1 for the integral math solution. Far more elegant than trying with a formula (at least for this type of problem).
helios
A: 

The Java tutorials clearly describe what you're looking for.

http://download.oracle.com/javase/tutorial/java/nutsandbolts/if.html

Nitrodist
+2  A: 

Its not fancy, but its simple and readable.

private static void homework(int n) {
    if (n > 9999) {
        System.out.println("Really big");
    } else {

        if (n <= 99 ) {
            System.out.println("Really small");
        } else if (n <= 999) {
            System.out.println("Hundred");
        } else if ( n <= 9999) {
            System.out.println("Thousand");
        } else {
            System.out.println("How did this get here? I'm not good with computer!");
        }

    }
}
BjornS