Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove digits from a number in Java [closed]

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.

like image 842
user3159331 Avatar asked Jan 04 '14 03:01

user3159331


People also ask

How do you trim a number in Java?

The trim() method in Java String is a built-in function that eliminates leading and trailing spaces. The Unicode value of space character is '\u0020'. The trim() method in java checks this Unicode value before and after the string, if it exists then removes the spaces and returns the omitted string.

How do you remove zeros from a number in Java?

Use the inbuilt replaceAll() method of the String class which accepts two parameters, a Regular Expression, and a Replacement String. To remove the leading zeros, pass a Regex as the first parameter and empty string as the second parameter. This method replaces the matched value with the given string.


6 Answers

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));
like image 78
Christian Tapia Avatar answered Oct 13 '22 06:10

Christian Tapia


try this

n = n % (int) Math.pow(10, (int) Math.log10(n));
like image 28
Evgeniy Dorofeev Avatar answered Oct 13 '22 07:10

Evgeniy Dorofeev


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

i %= (int) Math.pow(10, (int) Math.log10(i));
like image 29
Bohemian Avatar answered Oct 13 '22 05:10

Bohemian


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.

like image 37
BRPocock Avatar answered Oct 13 '22 05:10

BRPocock


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);
}
like image 2
Gene Avatar answered Oct 13 '22 06:10

Gene


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;
  }
}
like image 1
Darren Gilroy Avatar answered Oct 13 '22 07:10

Darren Gilroy