Return first digit of an integer

后端 未结 23 1570
醉梦人生
醉梦人生 2020-11-28 13:12

How in Java do you return the first digit of an integer.?

i.e.

345

Returns an int of 3.

相关标签:
23条回答
  • 2020-11-28 13:45

    The easiest way would be to use String.valueOf(Math.abs((long)x)).charAt(0) - that will give it you as a char1. To get that as an integer value, you could just subtract '0' (as in Unicode, '0' to '9' are contiguous).

    It's somewhat wasteful, of course. An alternative would just be to take the absolute value, then loop round dividing by 10 until the number is in the range 0-9. If this is homework, that's the answer I'd give. However, I'm not going to provide the code for it because I think it might be homework. However, if you provide comments and edit your answer to explain how you're doing and what problems you're running into, we may be able to help.


    1One sticky point to note is that the absolute value of Integer.MIN_VALUE can't be represented as an int - so you may should first convert to a long, then use Math.abs, then do arithmetic. That's why there's a cast there.

    0 讨论(0)
  • 2020-11-28 13:51

    Ignoring negative values leads to:

    (""+345).charAt(0);
    
    0 讨论(0)
  • 2020-11-28 13:51

    The missing recursive solution:

    int getFirstInt(int input) {
      if (input > 0 ? input < 10 : input > -10) {
        return input > 0 ? input : -input;
      }
      return getFirstInt(input / 10);
    }
    

    I wouldn't use the ternary operator in real life but - isn't it kind of beautiful? ;)

    0 讨论(0)
  • 2020-11-28 13:52

    To separate digits of an integer from left to right I use 2 different methods, the first one to count how many digits the integer is made up of and then I split them from left to right by dividing the integer by 10 raised to the power of the number of digits minus 1.

    //method to separate digits of an integer from left to right
    private static void separateDigits(int num){
        int numOfDigits = countNumberOfDigits(num);
        for (int numOfZeros = numOfDigits-1; numOfZeros >= 0 ; --numOfZeros){
            int divisor = (int) Math.pow(10, numOfZeros);
            System.out.print( Math.abs(num) / divisor + " // " );
            num %= divisor;
        }
    }
    
    //method to count number of digits
    private static int countNumberOfDigits(int num){
        int numOfDigits=0;
        //using absolute value of num allows method to work even with negative integers
        while(Math.abs(num) > 0){ 
            num = num / 10;
            numOfDigits++; //this counts the number of times the "while" loops
        }
        return numOfDigits;
    }
    

    No use of Arrays or recursive methods just simple division with "/" and "%".

    Invoking the method:

    public static void main(String args[]) {
    
    separateDigits( -123456789 );
    
    }
    

    yields: 1 // 2 // 3 // 4 // 5 // 6 // 7 // 8 // 9 //

    0 讨论(0)
  • 2020-11-28 13:53

    Fastest way would be :

    • Compute log of the abs(x), then get floor. Let's call it n.
    • Divide the number with 10^n
    0 讨论(0)
  • 2020-11-28 13:53
    int main(void) {
      int num = 3421;
    
      while (num*num + 10 - num*(1 + num) <= 0) {
        num *= (num - 0.9*num)/num;
      }
    
      std::cout << num << std::endl;
    }
    
    0 讨论(0)
提交回复
热议问题