2

I was wondering if anyone knew how to use mathematics to truncate a number from the left (remove digits one by one starting from the left).

I can use a simple division to truncate digits from the right:

int num = 10098;

while (num > 0){
    System.out.println(num);
    num /= 10;
}

This will output what I want:

10098
1009
100
10
1

But does anyone know a way to do this the other way around on integers and to truncate digits from the left without converting to String?

qcGold
  • 200
  • 2
  • 12
  • Doesn't `%` with powers of 10 do that? – blgt Apr 09 '15 at 14:11
  • @blgt: Yes, but you need to use a different RHS for each digit. – Jon Skeet Apr 09 '15 at 14:11
  • @JonSkeet True, but OP's code has a different LHS for each digit. Does it matter which side the loop updates? – blgt Apr 09 '15 at 14:15
  • The problem is the moment you remove the first number i.e `1` the next integer value will not be valid `(0098`) cannot be assigned to an int. So I don't think you will be able to do it using numbers – TheLostMind Apr 09 '15 at 14:15
  • 2
    @blgt: I think you're missing my point - currently there's `num /= 10` in each case. To go digit-by-digit the other way, you need `num % 10000` first, then `num % 1000` etc... and you've got to potentially work out what to start with. It's *at least* rather trickier code than the existing version, IMO. – Jon Skeet Apr 09 '15 at 14:16
  • @Jon Skeet: This has been my train of thought so far (I should have specified it in the question I guess), I was wondering if anyone knew a way around it.. – qcGold Apr 09 '15 at 14:20
  • 1
    @JonSkeet - Not just *where to start*. The other problem is `0098` is not a valid integer representation. So, unless we do some kind of operation on it, the leading 0s will be lost – TheLostMind Apr 09 '15 at 14:26

3 Answers3

4

Try n = n % (int) Math.pow(10, (int) Math.log10(n));. Found here.

public void test() {
    int n = 10098;
    while (n > 0) {
        System.out.println("n=" + n);
        n = n % (int) Math.pow(10, (int) Math.log10(n));
    }
}

prints

n=10098
n=98
n=8
Community
  • 1
  • 1
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

You can use the module operation %. The module is the remainder of the division.

You have to call it with with power of 10. So 10, 100, 1000, 10000

You can do it

int num = 10098;
int divided = num;
int module = 1;

while (divided > 0) {
    divided /= 10;
    module *= 10;
    System.out.println(num % module);    
}

Please note that if you don't left padding with zeros you will obtain 98 instead of 0098 or 098.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
0
int in = ...;

//the first number n : 10^n > in 
int mostSignificantDigit = 1;
int digits = 1;
while(mostSignificantDigit < in)
{
     mostSignificantDigit *= 10;
     digits++;
}

//create the numbers
int[] result = new int[digits];
int count = 0;
while(mostSignificantDigit != 0){
     result[count] = in % mostSignificantDigit;
     mostSignificantDigit /= 10;
}