9

How do I remove the first digit of an integer?

My input is an integer (for example i = 123456789).

I then want to remove the first digit, so that i equals 23456789.

user3159331
  • 99
  • 1
  • 1
  • 2
  • Also see [Return first digit of an integer](http://stackoverflow.com/questions/2051817/return-first-digit-of-an-integer) – Reimeus Jan 04 '14 at 03:24

7 Answers7

18

try this

n = n % (int) Math.pow(10, (int) Math.log10(n));
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • 1
    This is a neat solution (+1). You can also use `%=`. Also, you might not need the first `(int)` cast. – arshajii Jan 04 '14 at 03:28
15

Here is one way to do it:

  • Convert it to String
  • Take the substring without the first "digit"
  • Convert it to int

Code:

public static void main(String[] args)
{
    int x = 123456789;

    String x_str = Integer.toString(x);

    int new_x = Integer.parseInt(x_str.substring(1));

    System.out.println(new_x);
}

Output:

23456789

Note: This can be done in one line with

int x = 123456789;
int new_x = Integer.parseInt(Integer.toString(x).substring(1));

Edit:

To handle negative-case, check if number is positive or integer:

int new_x = Integer.parseInt(x > 0 ? 
    Integer.toString(x).substring(1) : Integer.toString(x).substring(2));
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
2

If you want to avoid the string conversion, you can find the high digit and subtract it.

public static void main(String[] args) {
    int x = 123456789;
    System.out.println("x = " + x);
    int hi = x, n = 0;
    while (hi > 9) {
        hi /= 10;
        ++n;
    }
    for (int i = 0; i < n; i++) hi *= 10;
    x -= hi;
    System.out.println("x with high digit removed = " + x);
}
Gene
  • 46,253
  • 4
  • 58
  • 96
  • Couldn't a log10 be used here? I would presume taking `floor(log10(x) + 1)` would return the number of digits it has, assuming x > 0. – Obicere Jan 04 '14 at 03:25
  • Sure. In some environments the transcendentals are much slower than integer arithmetic and pull in a math library that otherwise wouldn't be needed, so I always think of the integer solution first. – Gene Jan 04 '14 at 03:45
2

Here's the one-line, purely numeric solution:

i %= (int) Math.pow(10, (int) Math.log10(i));
Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

Alternate approach:

int stripLeading(int i) {
  if(i > 0) {
    return i - (int)Math.pow(10, (int)Math.log10(i));
  } else if(i > 0) {
    return i + (int)Math.pow(10, (int)Math.log(-i+1));
  } else {
    return 0;
  }
}
Darren Gilroy
  • 2,071
  • 11
  • 6
1

I think I remember the string-free version of this … although I totally agree with @Christian as how I would do it…

NOTE: as @Darren Gilroy pointed out, one must consider negatives and zero spocially, and my function fails to do so.

Of course % is a better solution also.

public static void main (String [] argv)
{
     final int x = 123456789;
     int newX = x;

     /* How many digits are there? */
     final double originalLog = Math.floor (Math.log10 (x));

     /* Let's subtract 10 to that power until the number is smaller */
     final int getRidOf = (int)Math.pow (10, originalLog);
     while (originalLog == Math.floor (Math.log10 (newX)))
     { newX -= getRidOf; }

     System.out.println (newX);
}

Poor profiling attempt:

Looping the above function without the println for 20,000,000,000 repeats in a for loop:

real    0m9.943s
user    0m9.890s
sys     0m0.028s

The same with Christian's far-easier-to-understand and perfectly functionable version, but for only 200,000,000 repeats (because I'm lazy and got tired of waiting):

real    0m18.581s
user    0m17.972s
sys     0m0.574s

So one might argue that constructing the String objects is probably slowing it down by roughly 200×, but that isn't a really finely-tuned profiling set-up.

BRPocock
  • 13,638
  • 3
  • 31
  • 50
0

If you want to go for simpler methods and without using String, then here's my simple take:

  1. Count number of digits int the integer.
  2. Divide the int by 10^n. n is the number of digits.
  3. Obtain absolute value of the result. //In case of (-)ve numbers.

For example

int i = 123456789;
int n = getDigitCount(i);
int r = Math.abs(i / (int)Math.pow(10,n)); //r stores result.

And you'd require this method:

int getDigitCount(int num)
{
    int c = 0;
    while(num > 0){
        num/=10;
        c++;
    }
    return c;
}
Hungry Blue Dev
  • 1,313
  • 16
  • 30
  • I am almost 5 years too late. It should be - int r = Math.abs(i / (int)Math.pow(10,n - 1)); --> "n - 1" in the end. – BrownRecluse Sep 20 '18 at 19:02