Removing numbers from a number in Java

How to remove the first digit of an integer?

My input is an integer (e.g. i = 123456789).

Then I want to delete the first digit, so I equal 23456789.

+7
java integer digit
source share
7 answers

Here is one way to do this:

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

The 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 on one line using

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

Edit:

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

 int new_x = Integer.parseInt(x > 0 ? Integer.toString(x).substring(1) : Integer.toString(x).substring(2)); 
+10
source share

try it

 n = n % (int) Math.pow(10, (int) Math.log10(n)); 
+10
source share

If you want to avoid 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); } 
+2
source share

Here's a one-line pure clean solution:

 i %= (int) Math.pow(10, (int) Math.log10(i)); 
+1
source share

Alternative 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; } } 
+1
source share

I think I remember a line-free version ... although I totally agree with @Christian how I do it ...

NOTE. As Darden Gilroy noted, you need to consider negatives and zero selectively, and my function cannot do this.

Of course, % also a better solution.

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

Bad profiling attempt:

Completing the specified function without println for 20,000,000,000 repetitions in a for loop:

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

The same thing with the Christian much simpler and more understandable version, but only 200,000,000 repetitions (because I'm lazy and tired of waiting):

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

So, it can be argued that building String objects probably slows it down by about 200 ×, but this is not a very finely tuned profiling setting.

0
source share

If you want to use simpler methods without using String , here is my simple example:

  • Count the number of digits int an integer.
  • Divide int by 10^n . n is the number of digits.
  • Get the absolute value of the result. // In the 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 will need this method:

 int getDigitCount(int num) { int c = 0; while(num > 0){ num/=10; c++; } return c; } 
0
source share

All Articles