GustavXIII
|
 |
«
Posted
2011-08-10 19:35:52 » |
|
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.
|
|
|
|
|
namrog84
|
 |
«
Reply #1 - Posted
2011-08-10 19:40:08 » |
|
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 1 2 3 4 5
| if(timer % 10 == 0){ } 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 \/
|
"Experience is what you get when you did not get what you wanted"
|
|
|
Riven
|
 |
«
Reply #2 - Posted
2011-08-10 19:42:47 » |
|
he wants the left-most digit: a quick and dirty solution would be: 1
| int digit = String.valueOf(Math.abs(i)).charAt(0)-'0'; |
how this relates to timers is a mystery to me.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
GustavXIII
|
 |
«
Reply #3 - Posted
2011-08-10 20:38:21 » |
|
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(?).
|
|
|
|
|
Gudradain
|
 |
«
Reply #4 - Posted
2011-08-10 20:44:36 » |
|
Divide by 1000
|
|
|
|
|
Riven
|
 |
«
Reply #5 - Posted
2011-08-10 20:53:08 » |
|
If I have the number 12009, I want to draw only the 1.
dividing by 1000 won't result in the example the OP posted.
|
|
|
|
delt0r
|
 |
«
Reply #6 - Posted
2011-08-12 12:23:27 » |
|
This would also work. 1 2 3 4 5 6
| 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 have no special talents. I am only passionately curious.--Albert Einstein
|
|
|
pitbuller
|
 |
«
Reply #7 - Posted
2011-08-12 12:28:29 » |
|
If I have the number 12009, I want to draw only the 1.
dividing by 1000 won't result in the example the OP posted. I think its pretty clear that op ask wrong question. I think he want seconds from milliseconds and somehow messed question.
|
|
|
|
|
Addictman
|
 |
«
Reply #8 - Posted
2011-08-12 12:39:06 » |
|
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.
|
|
|
|
|
JL235
|
 |
«
Reply #9 - Posted
2011-08-12 14:31:33 » |
|
Personally, I would go with Rivens String version, but here is an over-engineered alternative: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| 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.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Riven
|
 |
«
Reply #10 - Posted
2011-08-12 15:22:21 » |
|
faster faster! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| 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"); } } |

|
|
|
|
Roquen
|
 |
«
Reply #11 - Posted
2011-08-13 10:00:23 » |
|
If you really wanted to go overboard you could convert the linear search into a binary one.
|
|
|
|
|
counterp
|
 |
«
Reply #12 - Posted
2011-08-13 10:06:44 » |
|
only the original one Riven posted works properly  although addictman suggested using Math.abs for negatives so I guess he's good too
|
|
|
|
|
delt0r
|
 |
«
Reply #13 - Posted
2011-08-13 11:56:46 » |
|
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.
|
I have no special talents. I am only passionately curious.--Albert Einstein
|
|
|
Riven
|
 |
«
Reply #14 - Posted
2011-08-13 14:13:36 » |
|
If you really wanted to go overboard you could convert the linear search into a binary one.
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.
|
|
|
|
JL235
|
 |
«
Reply #15 - Posted
2011-08-13 16:03:26 » |
|
If you really wanted to go overboard you could convert the linear search into a binary one.
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... 1 2 3 4 5 6 7 8 9
| 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.
|
|
|
|
Roquen
|
 |
«
Reply #16 - Posted
2011-08-13 21:24:52 » |
|
I thought we were over-engineering the problem. Here's an untested entry: 1 2 3 4 5 6
| 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!
|
|
|
|
|
Abuse
|
 |
«
Reply #17 - Posted
2011-08-13 21:55:16 » |
|
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.
|
Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! 
|
|
|
counterp
|
 |
«
Reply #18 - Posted
2011-08-13 22:01:56 » |
|
If you really wanted to go overboard you could convert the linear search into a binary one.
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. Well in that case 1 2 3 4 5 6 7 8 9
| 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
|
|
|
|
|
Roquen
|
 |
«
Reply #19 - Posted
2011-08-13 22:32:19 » |
|
Well if you're doing anything like the stuff posted in the thread, then it's almost certain that: You're doing it wrongtm.
|
|
|
|
|
delt0r
|
 |
«
Reply #20 - Posted
2011-08-14 00:00:00 » |
|
So Roquen, what is the right way to be doing it?
|
I have no special talents. I am only passionately curious.--Albert Einstein
|
|
|
Riven
|
 |
«
Reply #21 - Posted
2011-08-14 00:04:39 » |
|
I'm fairly certain he means that the 'problem' defined by the OP has some flaws, so the solution will be inherently wrong.
|
|
|
|
Roquen
|
 |
«
Reply #22 - Posted
2011-08-14 00:11:41 » |
|
Yes, that's exactly what I meant.
|
|
|
|
|
|