Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: counting digits in a float number

Is there any worthy Ruby method to count the number of digits in a float? Also, how do I specify the precise when to_s float numbers?

like image 676
gmile Avatar asked Jan 24 '23 03:01

gmile


1 Answers

# Number of digits

12345.23.to_s.split("").size -1 #=> 7

# The precious part

("." + 12345.23.to_s.split(".")[1]).to_f #=> .023

# I would rather used 
# 12345.23 - 12345.23.to_i 
# but this gives 0.22999999999563
like image 198
khelll Avatar answered Jan 25 '23 18:01

khelll