How to find a length of an integer | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to find a length of an integer

we really have to convert the integer into a string??i dont understand why all of them are doing it.......

2nd Dec 2017, 5:17 AM
Siva Lakshmanan
Siva Lakshmanan - avatar
6 Answers
+ 5
int nb = 1; int bit = 1; //let's say your number is n while(bit <= n){ ++nb; bit *= 10; }
2nd Dec 2017, 6:49 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 4
An integer does not have a length property or a method that returns the length or number of digits that the number has. Most will convert the number to a String and then use the String length to get the number of digits the number has. The other option is to use some mathematical method that will return the number of digits that a number has. This is what the code I posted does in the getNumberOfDigits(int number) method.
2nd Dec 2017, 5:30 AM
ChaoticDawg
ChaoticDawg - avatar
2nd Dec 2017, 5:23 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
thank u sir now i understand that.......
2nd Dec 2017, 5:33 AM
Siva Lakshmanan
Siva Lakshmanan - avatar
0
give me a simple answer im just a beginnner this code is difficult for me sir....
2nd Dec 2017, 5:25 AM
Siva Lakshmanan
Siva Lakshmanan - avatar
0
find the no. of digits
2nd Dec 2017, 10:17 AM
Siva Lakshmanan
Siva Lakshmanan - avatar