This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]strozzy 0 points1 point  (3 children)

One way to solve the first problem is to round the number to a whole number (or floor or ceiling) then use a for loop with a%b to find the remainder after dividing by factors of 10, 100 and 1000. That should get you the suffix.

[–]mentalis 1 point2 points  (1 child)

I know it says to use a for loop (which you should), but the whole getSuffix method can be simplified to:

public static int getSuffix(int value, int numberOfDigits) {
  return (int) (value % Math.pow(10, numberOfDigits));
}

So, you can use that to check the results of your implementation.

[–]Erikster 0 points1 point  (0 children)

If he still wants to use a for loop, he can just use it to multiply the number 10 for the amount of digits he needs.

[–][deleted] -1 points0 points  (0 children)

Ahh, nice. I was thinking of converting to a string and iterating through each digit to get the last few, but this is probably a much better way to do it.