+ 2

Just a straight question

I wanna use common logarithm to count number of digits. it doesn't work for me here is the code https://code.sololearn.com/cDYRVXFWciC7/?ref=app

3rd Oct 2022, 12:02 PM
Davinder Kumar
Davinder Kumar - avatar
2 ответов
+ 4
Floor() has to be lowercase. Precede log10 with the library name. int len = (int)Math.floor(Math.log10(n)+1); Since you are converting to int, you don't really need to call floor. Beware of the limitation of using log. It cannot work with numbers that are less than or equal to zero. To work around that limitation I use absolute value and I check for the special case of 0. int len = n==0? 1 : (int)Math.log10(Math.abs(n))+1;
3rd Oct 2022, 12:44 PM
Brian
Brian - avatar
3rd Oct 2022, 12:12 PM
Lisa
Lisa - avatar