Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (10.2k points)

Is there a neater way for getting the length of an int than this method?

int length = String.valueOf(1000).length();

1 Answer

0 votes
by (46k points)

Your String-based clarification is absolutely OK, there is zero "un-neat" regarding it. You have to understand that mathematically, numbers don't have a length, nor do they have digits. Length and digits are both parts of a physical representation of a number in a specific base, i.e. a String.

A logarithm-based answer does (some of) the identical things the String-based one does internally, and apparently does so (insignificantly) ready because it only produces the length and overlooks the digits. But I wouldn't count it more obvious in intent - and that's the common major factor.

Related questions

0 votes
1 answer
asked Aug 6, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 26, 2019 in Java by Shubham (3.9k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...