Return last 5 digits of a number

前端 未结 3 765
被撕碎了的回忆
被撕碎了的回忆 2021-02-14 05:31

How do you only display the last 5 digits of a number?

Example input:

123456789

Would return: 56789

相关标签:
3条回答
  • 2021-02-14 05:39

    Let's assume that required number to convert is an integer. Then you can use a modular mathematic - you can the number convert to the module with base 100 000. That means that only last 5 digits will be kept. The conversion can be done by an operator for remainder of division, the operator is %.

    The code is:

    int x = 123456;
    int lastDigits = x % 100000;
    
    0 讨论(0)
  • 2021-02-14 05:40

    One approach is to do the following:

    1) Convert the number to a string
    2) Make sure the string has more than 5 digits.
    3) If it does, get the last five characters in the string.

    The above three steps have links that show you how implement them. Since this is homework, getting it to work is left as an exercise (channeling my former TA).

    0 讨论(0)
  • 2021-02-14 05:47

    I suggest using the modulus if working with integerss:

    int someNumber = 123456789;
    int lastFive = someNumber % 100000;
    

    Something like that

    0 讨论(0)
提交回复
热议问题