First Number from Integer

Hi!
I want to draw just the first Number from Integer.
If I have the number 12009, I want to draw only the 1.

In C there was something like number%d or something like that.
How does it work for java? It is because I want to make a Timer for the game.

in java the % is called a modulus

so lets say you have 13%10 that would equal 3
or 11%10 would equal 1, it gives the remainder after the division.

this will occur every time timer is divisible by 10, so 1,2,3,4,5,6,7,8,9 would NOT trigger it


//loop
if(timer % 10 == 0){
    //ticks every 10th interval
}
timer++;

This is 1 of many way some people do some sort of interval timer.

You could also just set up a simple little loop to break each number into something separate

How exactly do you want to implement your timer? is it forever counting up like elapsed time? or firing/action interval? or whatever, as there may be a better solution for your given scenario.

/ what he said works too /

he wants the left-most digit:

a quick and dirty solution would be:

int digit = String.valueOf(Math.abs(i)).charAt(0)-'0';

how this relates to timers is a mystery to me.

Ok thanks!
@namrog In Slick I using delta. If delta is 1000 its 1 second. But I cant show 1000 in the menu so I have to reduce it to the first digit(?).

Divide by 1000

dividing by 1000 won’t result in the example the OP posted.

This would also work.


import static Math.*;

public int firstDig(double x){
   double divisor=pow(10,floor(log10(x)));
   return (int)(x/divisor);
}

This can be extended easily for any number base.

I think its pretty clear that op ask wrong question. I think he want seconds from milliseconds and somehow messed question.

int a = 12009;
int b = (int)(a / Math.pow(10, (int)Math.log10(a))); // b --> 1

Math.abs() if a is negative.

Edit: Ok, just saw delt0r posted the same.

Personally, I would go with Rivens String version, but here is an over-engineered alternative:

public void firstInt( int n ) {
    boolean positive = false;
    if ( n < 0 ) {
        n = Math.abs(n);
        positive = true;
    }

    for ( int base = 10; base < 1000000000; base *= 10 ) {
        if ( n < base ) {
            int result = (int) ( n / (base/10.0) ) ;
            return positive ? result : -result ;
        }
    }

    throw IllegalStateException( "should never be able to reach this point" );
}

It could also be expanded into a long list of if’s.

faster faster!

   public static int firstDigit(int n)
   {
      int mul;

      if (n < 0)
      {
         mul = -1;
         n = -n;
      }
      else
      {
         mul = +1;
      }

      for (int base = 10; base <= 1000000000; base *= 10)
      {
         if (n < base)
         {
            return mul * n * 10 / base;
         }
      }

      throw new IllegalStateException("should never be able to reach this point");
   }
}

:stuck_out_tongue:

If you really wanted to go overboard you could convert the linear search into a binary one.

only the original one Riven posted works properly :stuck_out_tongue: although addictman suggested using Math.abs for negatives so I guess he’s good too

It won’t if the argument is negative. You get “-” or whatever negative convention the local uses. I tested my stuff in R --seem to work fine.

While this is true, I think it’s likely that in this case, most values will be less than 100.000, in which case it’s pretty hard to beat the linear search.

You could increment the base by 100 instead of 10, something like…


for (int base = 10; base <= 1000000000; base *= 100) {
    if (n < base) {
        if ( n < base/10 ) {
            return mul * n * 10 / (base/10);
        } else {
            return mul * n * 10 / base;
        }
    }
}

This would almost half the number checks.

I thought we were over-engineering the problem. Here’s an untested entry:


  public static int firstDecimalDigit(int n)
  {
    n = Math.abs(n);
    
    return (int)Math.floor(n*Math.pow(10, -((int)Math.log10(n))));
  }

edit: I somehow missed the two previous in the same vein…dang!

I’m sure you’re all aware that negation does not work for Integer.MIN_VALUE (and consequently neither does Math.abs(int)).

So all of the above implementations will malfunction in all kinds of interesting ways when the input n=Integer.MIN_VALUE.

Well in that case

	private static final int digits[] = new int[200001];
	static {
		for (int i = 0; i < digits.length; i++)
			digits[i] = String.valueOf(Math.abs(i - 100000)).charAt(0)-'0';
	}

	public static int lookup(int n) {
		return digits[n + 100000];
	}

lololololololol

-100000 to 100000

if you were worried about memory you could convert it to byte or something, or even do some bitshifting stuff to get maximum value out of the integers

Well if you’re doing anything like the stuff posted in the thread, then it’s almost certain that: You’re doing it wrongtm.