Lotus
Lotus

Reputation: 1

Java (hundreds, thousands, etc)

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

Upvotes: 0

Views: 467

Answers (6)

BjornS
BjornS

Reputation: 1024

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!");
        }

    }
}

Upvotes: 2

user85421
user85421

Reputation: 29680

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.

Upvotes: 1

Matthew Whitrock
Matthew Whitrock

Reputation: 46

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.

Upvotes: 2

Yuval Adam
Yuval Adam

Reputation: 165242

Simple math:

Math.pow(10, Math.floor(Math.log(n) / Math.log(10))) // for given n

Upvotes: 1

Stephen
Stephen

Reputation: 6087

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.)

Upvotes: 4

Steve McLeod
Steve McLeod

Reputation: 52448

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.

Upvotes: 5

Related Questions