How to count the number of digits in a number with Ruby

2 Answers

0 votes
n = 38729

total_digit = Math.log10(n).to_i + 1

print total_digit



#
# run:
# 
# 5
#

 



answered Oct 17, 2021 by avibootz
0 votes
def num_digits(n)
    return Math.log10(n).to_i + 1
end
 
n = 38729
 
puts num_digits(n)
 
 
 
 
 
#
# run:
# 
# 5
#

 



answered Oct 17, 2021 by avibootz

Related questions

1 answer 244 views
1 answer 196 views
1 answer 197 views
1 answer 195 views
2 answers 238 views
...