添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have the following code to check if the value is Int or null and further check if it is below a certain number:

            binding.RootLayout.forEach {
                if (it is EditText) {
                    val intOrNull = it.text.toString().toIntOrNull()
                    if (intOrNull == null) {
                        count += 1
                    } else if (intOrNull > 100000) {
                        overTheLimitExist = true

The problem is, when I enter some number like 123456789 or below, it correctly identify it as (1) Int and (2) above the preset limit. However, if I enter a larger number such as 12345678900 it incorrectly identify it as null.

I search online for the toIntOrNull but they didn't say anything about a limit to the function.

Max limit of Int is approximately 2*10^9 (4byte). Over that until approx 4*10^18 use Long(8byte). Or else BigInteger which can store number upto infinity :P (just kidding it can store upto ~18GB of RAM). – Animesh Sahu Jan 10, 2021 at 8:14

12345678900 is larger than Integer's max value (2^31-1) , hence the implementation of toIntOrNull returns null, as 12345678900 doesn't fit in 4 bytes.

You can use toLongOrNull or toBigIntegerOrNull for really big numbers.

I think this problem is for limit of int type
(-2,147,483,648 to 2,147,483,647) you should use another type such as "Long" or BigNumber java class. 
you can test 1 bigger than this range (2147483648) or 1 smaller (2147483646) and see the result.
I hope this help to you. 
sorry for my English.
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.